Skip to content

Commit 06ebdbb

Browse files
committed
Fix UT
1 parent 0f8ec11 commit 06ebdbb

9 files changed

Lines changed: 66 additions & 20 deletions

File tree

modules/Nncase.Modules.NTT/Evaluator/Distributed/Boxing.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public Cost Visit(ICostEvaluateContext context, Boxing target)
101101
cost = new Cost()
102102
{
103103
[CostFactorNames.MemoryLoad] = CostUtility.GetMemoryAccess(distributedType),
104+
[CostFactorNames.MemoryStore] = CostUtility.GetMemoryAccess(distributedType),
104105
};
105106
break;
106107
}
@@ -112,6 +113,7 @@ public Cost Visit(ICostEvaluateContext context, Boxing target)
112113
default:
113114
cost = new Cost()
114115
{
116+
[CostFactorNames.MemoryLoad] = CostUtility.GetMemoryAccess(distributedType),
115117
[CostFactorNames.MemoryStore] = CostUtility.GetMemoryAccess(distributedType),
116118
[CostFactorNames.Synchronization] = synchronizeCost,
117119
};

ntt/include/nncase/ntt/kernels/pack.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "../apply.h"
1717
#include "../tensor_traits.h"
1818
#include "../ukernels/u_pack.h"
19+
#include "../vector.h"
1920

2021
namespace nncase::ntt {
2122
namespace detail {
@@ -157,7 +158,8 @@ template <Tensor TIn, Tensor TOut> class pack_impl<TIn, TOut, 1> {
157158
template <Tensor TIn, class TOut, FixedDimensions TAxes>
158159
void pack(const TIn &input, TOut &&output, const TAxes &axes) noexcept {
159160
using TVec = typename std::decay_t<TOut>::element_type;
160-
static_assert(TVec::rank() == TAxes::rank(),
161+
static_assert(TVec::rank() ==
162+
vector_rank_v<typename TIn::value_type> + TAxes::rank(),
161163
"Output vector rank must match axes rank");
162164
detail::pack_impl<TIn, std::decay_t<TOut>, TAxes::rank()> impl;
163165
impl(input, output, axes);

ntt/include/nncase/ntt/ukernels/u_pack.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ template <class T1, class T2, bool Arch> struct u_pack_policy {
2626
static constexpr size_t unroll = 4;
2727
};
2828

29-
template <bool Arch, Scalar TIn, Vector TOut> class u_pack {
29+
template <bool Arch, ScalarOrVector TIn, Vector TOut> class u_pack {
3030
public:
3131
template <Dimension TM, Dimension TN, Dimension TMStrides>
3232
constexpr void operator()(const TIn *input, const TM &M, const TN &N,
@@ -82,7 +82,7 @@ class u_pack2d {
8282
};
8383
} // namespace ukernels
8484

85-
template <Scalar TIn, Dimension TM, Dimension TN, Dimension TMStrides,
85+
template <ScalarOrVector TIn, Dimension TM, Dimension TN, Dimension TMStrides,
8686
Vector TOut>
8787
constexpr void u_pack(const TIn *input, const TM &M, const TN &N,
8888
const TMStrides &m_strides, TOut *output) noexcept {

ntt/include/nncase/ntt/vector.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#pragma once
1616
#include "detail/shape_storage.h"
1717
#include "detail/vector_storage.h"
18+
#include "nncase/ntt/dimension.h"
1819
#include "tensor_traits.h"
1920
#include <type_traits>
2021

@@ -45,8 +46,11 @@ class basic_vector
4546
return Lanes{}.template at<Index>();
4647
}
4748

48-
static basic_vector<T, Lanes> from_scalar(T value) noexcept;
49-
static basic_vector<T, Lanes> unaligned_load_from(const T *ptr) noexcept;
49+
template <ScalarOrVector U>
50+
static basic_vector<T, Lanes> from_scalar(U value) noexcept;
51+
52+
template <ScalarOrVector U>
53+
static basic_vector<T, Lanes> unaligned_load_from(const U *ptr) noexcept;
5054

5155
constexpr basic_vector() noexcept = default;
5256
constexpr basic_vector(const buffer_type &buffer) noexcept
@@ -118,4 +122,14 @@ template <Vector T, size_t... Lanes> struct replace_lanes_type {
118122

119123
template <Vector T, size_t... Lanes>
120124
using replace_lanes_t = typename replace_lanes_type<T, Lanes...>::type;
125+
126+
template <class T> struct vector_rank {
127+
static constexpr auto value = dim_zero;
128+
};
129+
130+
template <Vector T> struct vector_rank<T> {
131+
static constexpr auto value = fixed_dim_v<T::rank()>;
132+
};
133+
134+
template <class T> constexpr inline auto vector_rank_v = vector_rank<T>::value;
121135
} // namespace nncase::ntt

ntt/include/nncase/ntt/vector_ops.h

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -547,19 +547,29 @@ namespace nncase::ntt::vector_ops {
547547
template <Vector TVector> struct vload_scalar {
548548
using T = typename TVector::element_type;
549549

550-
constexpr TVector operator()(const T &value) const noexcept {
550+
template <ScalarOrVector U>
551+
constexpr TVector operator()(const U &value) const noexcept {
552+
const auto domain =
553+
TVector::shape()
554+
.template slice<0, TVector::rank() - vector_rank_v<U>>();
555+
551556
TVector vec{};
552-
ntt::apply(vec.shape(), [&](auto index) { vec(index) = value; });
557+
ntt::apply(domain, [&](auto index) { vec(index) = value; });
553558
return vec;
554559
}
555560
};
556561

557562
template <Vector TVector> struct vunaligned_load {
558563
using T = typename TVector::element_type;
559564

560-
constexpr TVector operator()(const T *ptr) const noexcept {
565+
template <ScalarOrVector U>
566+
constexpr TVector operator()(const U *ptr) const noexcept {
567+
const auto domain =
568+
TVector::shape()
569+
.template slice<0, TVector::rank() - vector_rank_v<U>>();
570+
561571
TVector vec{};
562-
ntt::apply(vec.shape(), [&](auto index) { vec(index) = *ptr++; });
572+
ntt::apply(domain, [&](auto index) { vec(index) = *ptr++; });
563573
return vec;
564574
}
565575
};
@@ -600,13 +610,15 @@ struct vmma {
600610

601611
namespace nncase::ntt {
602612
template <Scalar T, FixedShape Lanes>
603-
basic_vector<T, Lanes> basic_vector<T, Lanes>::from_scalar(T value) noexcept {
613+
template <ScalarOrVector U>
614+
basic_vector<T, Lanes> basic_vector<T, Lanes>::from_scalar(U value) noexcept {
604615
return vector_ops::vload_scalar<basic_vector<T, Lanes>>()(value);
605616
}
606617

607618
template <Scalar T, FixedShape Lanes>
619+
template <ScalarOrVector U>
608620
basic_vector<T, Lanes>
609-
basic_vector<T, Lanes>::unaligned_load_from(const T *ptr) noexcept {
621+
basic_vector<T, Lanes>::unaligned_load_from(const U *ptr) noexcept {
610622
return vector_ops::vunaligned_load<basic_vector<T, Lanes>>()(ptr);
611623
}
612624

src/Nncase.Core/Tensor.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,13 @@ public Tensor CastTo(DataType type, CastMode castMode, long[] dimensions)
554554
/// <returns>Memory handle.</returns>
555555
public abstract MemoryHandle PinBuffer();
556556

557+
/// <summary>
558+
/// Reshapes the current tensor to new dimensions, using the same backing storage.
559+
/// </summary>
560+
/// <param name="dimensions">An span of integers that represent the size of each dimension of the DenseTensor to create.</param>
561+
/// <returns>A new tensor that reinterprets backing Buffer of this tensor with different dimensions.</returns>
562+
public abstract Tensor Reshape(ReadOnlySpan<long> dimensions);
563+
557564
/// <inheritdoc/>
558565
public IEnumerator GetEnumerator()
559566
{

src/Nncase.Core/TensorOfT.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public T GetValue(long index)
180180
/// </summary>
181181
/// <param name="dimensions">An span of integers that represent the size of each dimension of the DenseTensor to create.</param>
182182
/// <returns>A new tensor that reinterprets backing Buffer of this tensor with different dimensions.</returns>
183-
public Tensor<T> Reshape(ReadOnlySpan<long> dimensions)
183+
public override Tensor<T> Reshape(ReadOnlySpan<long> dimensions)
184184
{
185185
if (Length != TensorUtilities.GetProduct(dimensions))
186186
{

src/Nncase.Evaluator/Tensors/Unpack.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ public IValue Visit(IEvaluateContext context, Unpack target)
2121
{
2222
var dt = context.CurrentCall.Arguments[Unpack.Input.Index].CheckedDataType;
2323
var elementType = dt is VectorType vt ? vt.ElemType : dt;
24-
var oldLanesCount = ((VectorType)dt).Lanes.Count;
24+
var oldLanesCount = dt switch
25+
{
26+
VectorType vt2 => vt2.Lanes.Count,
27+
MaskVectorType => 1,
28+
_ => throw new InvalidOperationException($"Unsupported input type: {dt}"),
29+
};
2530
if (elementType == DataTypes.Float8E4M3 || elementType == DataTypes.Float8E5M2)
2631
{
2732
var newType = new VectorType(DataTypes.UInt8, target.Lanes.ToArray());

src/Nncase.Tests/Targets/UnitTestCPUKernels.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ public async Task TestReshard(long[] shape, int[] hierarchy, List<int[][]> sbps,
474474

475475
var post = IR.F.Distributed.Boxing(boxed, inputType);
476476
post.Metadata = new Passes.Distributed.AutoDistributedMetaData() { Skip = true };
477-
await RunCases($"Theory{count}", feedDict, new[] { post });
477+
await RunCases($"Theory{count}", feedDict, new[] { post }, enableAutoDist: false);
478478
}
479479

480480
[Theory]
@@ -1898,7 +1898,7 @@ public async Task TestVectorizeScatterND(long[] inShape, long[] indicesShape, lo
18981898
await RunCases($"Theory{count}", feedDict, posts);
18991899
}
19001900

1901-
internal async Task RunCases(string dumpDir, Dictionary<IVar, IValue> feedDict, IEnumerable<BaseExpr> posts, Dictionary<IVar, IValue>? feedDictRT = null)
1901+
internal async Task RunCases(string dumpDir, Dictionary<IVar, IValue> feedDict, IEnumerable<BaseExpr> posts, Dictionary<IVar, IValue>? feedDictRT = null, bool enableAutoDist = true)
19021902
{
19031903
var postArray = posts.ToArray();
19041904
using var pinner = new ExprPinner(postArray);
@@ -1908,11 +1908,11 @@ internal async Task RunCases(string dumpDir, Dictionary<IVar, IValue> feedDict,
19081908
System.Console.WriteLine(CompilerServices.Print(postArray[i]));
19091909
#endif
19101910
var kernelCase = new CpuKernelCase($"Case{i}", new Fusion("kernel", CPUTarget.Kind, postArray[i], feedDict.Keys.ToArray()), feedDict.Keys.ToArray(), feedDict.Values.Select(v => v.AsTensor()).ToArray(), feedDictRT?.Values.Select(v => v.AsTensor()).ToArray() ?? []);
1911-
await Run(dumpDir, kernelCase);
1911+
await Run(dumpDir, kernelCase, enableAutoDist: enableAutoDist);
19121912
}
19131913
}
19141914

1915-
internal async Task Run(string dumpDir, CpuKernelCase kernelCase)
1915+
internal async Task Run(string dumpDir, CpuKernelCase kernelCase, bool enableAutoDist = true)
19161916
{
19171917
using var dumpScope = new Diagnostics.DumpScope(Path.Join(dumpDir, kernelCase.Name), CompileOptions.DumpFlags);
19181918

@@ -1947,7 +1947,7 @@ internal async Task Run(string dumpDir, CpuKernelCase kernelCase)
19471947
}
19481948
}
19491949
#endif
1950-
await Compile(module);
1950+
await Compile(module, enableAutoDist: enableAutoDist);
19511951
var (kmodel_path, _) = Testing.BuildKModel("test", module, CompileSession, false);
19521952
Tensor[] actuals;
19531953
if (kernelCase.RTInputs.Any())
@@ -1974,12 +1974,16 @@ internal async Task Run(string dumpDir, CpuKernelCase kernelCase)
19741974
}
19751975
}
19761976

1977-
private async Task Compile(IRModule module)
1977+
private async Task Compile(IRModule module, bool enableAutoDist = true)
19781978
{
19791979
var pmgr = CompileSession.CreatePassManager("pmgr");
19801980
var compiler = (Nncase.Compiler.Compiler)CompileSession.Compiler;
19811981
compiler.TargetIndependentPass(pmgr);
1982-
compiler.AutoDistributedPass(pmgr);
1982+
if (enableAutoDist)
1983+
{
1984+
compiler.AutoDistributedPass(pmgr);
1985+
}
1986+
19831987
compiler.AutoTilingPass(pmgr);
19841988
compiler.TIRPass(pmgr);
19851989
await pmgr.RunAsync(module);

0 commit comments

Comments
 (0)