diff --git a/scripts/generate_torch_ops.py b/scripts/generate_torch_ops.py index 9e45a5f4e..6b73a7c87 100644 --- a/scripts/generate_torch_ops.py +++ b/scripts/generate_torch_ops.py @@ -1118,11 +1118,11 @@ def _generate_base_header(name: str, ops: list[Op]) -> str: init_pieces.append(f" has_{api_name}_{{{api_name}.has_value()}}") init_pieces.append( f" {api_name}_shape_{{{api_name} ? " - f"{api_name}->shape() : Tensor::Shape{{}}}}" + f"Tensor::Shape{{{api_name}->shape()}} : Tensor::Shape{{}}}}" ) init_pieces.append( f" {api_name}_strides_{{{api_name} ? " - f"{api_name}->strides() : Tensor::Strides{{}}}}" + f"Tensor::Strides{{{api_name}->strides()}} : Tensor::Strides{{}}}}" ) init_pieces.append( f" {api_name}_type_{{{api_name} ? " diff --git a/src/base/add.h b/src/base/add.h index 985e6ed03..d21ecdc16 100644 --- a/src/base/add.h +++ b/src/base/add.h @@ -4,7 +4,6 @@ #include #include #include -#include #include "operator.h" @@ -68,7 +67,7 @@ class Add : public Operator { auto input_shape = input.shape(); auto other_shape = other.shape(); auto ndim = std::max(input_shape.size(), other_shape.size()); - std::vector out_shape(ndim, 1); + typename TensorLike::Shape out_shape(ndim, 1); for (std::size_t i = 0; i < ndim; ++i) { auto input_dim = i < ndim - input_shape.size() diff --git a/src/base/batch_norm_elemt.h b/src/base/batch_norm_elemt.h index c5e406e2a..5da418ebe 100644 --- a/src/base/batch_norm_elemt.h +++ b/src/base/batch_norm_elemt.h @@ -25,12 +25,15 @@ class BatchNormElemt : public Operator { out_strides_{out.strides()}, out_type_{out.dtype()}, has_weight_{weight.has_value()}, - weight_shape_{weight ? weight->shape() : Tensor::Shape{}}, - weight_strides_{weight ? weight->strides() : Tensor::Strides{}}, + weight_shape_{weight ? Tensor::Shape{weight->shape()} + : Tensor::Shape{}}, + weight_strides_{weight ? Tensor::Strides{weight->strides()} + : Tensor::Strides{}}, weight_type_{weight ? weight->dtype() : DataType::kFloat32}, has_bias_{bias.has_value()}, - bias_shape_{bias ? bias->shape() : Tensor::Shape{}}, - bias_strides_{bias ? bias->strides() : Tensor::Strides{}}, + bias_shape_{bias ? Tensor::Shape{bias->shape()} : Tensor::Shape{}}, + bias_strides_{bias ? Tensor::Strides{bias->strides()} + : Tensor::Strides{}}, bias_type_{bias ? bias->dtype() : DataType::kFloat32}, eps_{eps}, device_index_{out.device().index()} {} diff --git a/src/base/binary_cross_entropy.h b/src/base/binary_cross_entropy.h index 6218f2ee0..5856fed00 100644 --- a/src/base/binary_cross_entropy.h +++ b/src/base/binary_cross_entropy.h @@ -22,8 +22,10 @@ class BinaryCrossEntropy : public Operator { out_strides_{out.strides()}, out_type_{out.dtype()}, has_weight_{weight.has_value()}, - weight_shape_{weight ? weight->shape() : Tensor::Shape{}}, - weight_strides_{weight ? weight->strides() : Tensor::Strides{}}, + weight_shape_{weight ? Tensor::Shape{weight->shape()} + : Tensor::Shape{}}, + weight_strides_{weight ? Tensor::Strides{weight->strides()} + : Tensor::Strides{}}, weight_type_{weight ? weight->dtype() : DataType::kFloat32}, reduction_{reduction}, device_index_{out.device().index()} {} diff --git a/src/base/binary_cross_entropy_backward.h b/src/base/binary_cross_entropy_backward.h index 2764896fd..3d0d00948 100644 --- a/src/base/binary_cross_entropy_backward.h +++ b/src/base/binary_cross_entropy_backward.h @@ -26,8 +26,10 @@ class BinaryCrossEntropyBackward : public Operator { grad_input_strides_{grad_input.strides()}, grad_input_type_{grad_input.dtype()}, has_weight_{weight.has_value()}, - weight_shape_{weight ? weight->shape() : Tensor::Shape{}}, - weight_strides_{weight ? weight->strides() : Tensor::Strides{}}, + weight_shape_{weight ? Tensor::Shape{weight->shape()} + : Tensor::Shape{}}, + weight_strides_{weight ? Tensor::Strides{weight->strides()} + : Tensor::Strides{}}, weight_type_{weight ? weight->dtype() : DataType::kFloat32}, reduction_{reduction}, device_index_{grad_input.device().index()} {} diff --git a/src/base/clamp.h b/src/base/clamp.h index 5b1665d9a..d1b1bde7e 100644 --- a/src/base/clamp.h +++ b/src/base/clamp.h @@ -30,12 +30,12 @@ class Clamp : public Operator { out_strides_{out.strides()}, out_type_{out.dtype()}, has_min_{min.has_value()}, - min_shape_{min ? min->shape() : Tensor::Shape{}}, - min_strides_{min ? min->strides() : Tensor::Strides{}}, + min_shape_{min ? Tensor::Shape{min->shape()} : Tensor::Shape{}}, + min_strides_{min ? Tensor::Strides{min->strides()} : Tensor::Strides{}}, min_type_{min ? min->dtype() : DataType::kFloat32}, has_max_{max.has_value()}, - max_shape_{max ? max->shape() : Tensor::Shape{}}, - max_strides_{max ? max->strides() : Tensor::Strides{}}, + max_shape_{max ? Tensor::Shape{max->shape()} : Tensor::Shape{}}, + max_strides_{max ? Tensor::Strides{max->strides()} : Tensor::Strides{}}, max_type_{max ? max->dtype() : DataType::kFloat32}, device_index_{out.device().index()} {} diff --git a/src/base/clip.h b/src/base/clip.h index b20bd6b6e..72337ac91 100644 --- a/src/base/clip.h +++ b/src/base/clip.h @@ -30,12 +30,12 @@ class Clip : public Operator { out_strides_{out.strides()}, out_type_{out.dtype()}, has_min_{min.has_value()}, - min_shape_{min ? min->shape() : Tensor::Shape{}}, - min_strides_{min ? min->strides() : Tensor::Strides{}}, + min_shape_{min ? Tensor::Shape{min->shape()} : Tensor::Shape{}}, + min_strides_{min ? Tensor::Strides{min->strides()} : Tensor::Strides{}}, min_type_{min ? min->dtype() : DataType::kFloat32}, has_max_{max.has_value()}, - max_shape_{max ? max->shape() : Tensor::Shape{}}, - max_strides_{max ? max->strides() : Tensor::Strides{}}, + max_shape_{max ? Tensor::Shape{max->shape()} : Tensor::Shape{}}, + max_strides_{max ? Tensor::Strides{max->strides()} : Tensor::Strides{}}, max_type_{max ? max->dtype() : DataType::kFloat32}, device_index_{out.device().index()} {} diff --git a/src/base/conv_infinilm.h b/src/base/conv_infinilm.h index 6b84a5993..ca4db7b2a 100644 --- a/src/base/conv_infinilm.h +++ b/src/base/conv_infinilm.h @@ -23,8 +23,10 @@ class ConvInfinilm : public Operator { weight_strides_{weight.strides()}, out_shape_{out.shape()}, out_strides_{out.strides()}, - bias_shape_{bias.has_value() ? bias->shape() : Tensor::Shape{}}, - bias_strides_{bias.has_value() ? bias->strides() : Tensor::Strides{}}, + bias_shape_{bias.has_value() ? Tensor::Shape{bias->shape()} + : Tensor::Shape{}}, + bias_strides_{bias.has_value() ? Tensor::Strides{bias->strides()} + : Tensor::Strides{}}, input_type_{input.dtype()}, weight_type_{weight.dtype()}, out_type_{out.dtype()}, diff --git a/src/base/diff.h b/src/base/diff.h index 43b79f572..48d0c02a6 100644 --- a/src/base/diff.h +++ b/src/base/diff.h @@ -19,12 +19,16 @@ class Diff : public Operator { out_strides_{out.strides()}, out_type_{out.dtype()}, has_prepend_{prepend.has_value()}, - prepend_shape_{prepend ? prepend->shape() : Tensor::Shape{}}, - prepend_strides_{prepend ? prepend->strides() : Tensor::Strides{}}, + prepend_shape_{prepend ? Tensor::Shape{prepend->shape()} + : Tensor::Shape{}}, + prepend_strides_{prepend ? Tensor::Strides{prepend->strides()} + : Tensor::Strides{}}, prepend_type_{prepend ? prepend->dtype() : DataType::kFloat32}, has_append_{append.has_value()}, - append_shape_{append ? append->shape() : Tensor::Shape{}}, - append_strides_{append ? append->strides() : Tensor::Strides{}}, + append_shape_{append ? Tensor::Shape{append->shape()} + : Tensor::Shape{}}, + append_strides_{append ? Tensor::Strides{append->strides()} + : Tensor::Strides{}}, append_type_{append ? append->dtype() : DataType::kFloat32}, n_{n}, dim_{dim}, diff --git a/src/base/embedding.h b/src/base/embedding.h index c3dd16b0e..8d10243bb 100644 --- a/src/base/embedding.h +++ b/src/base/embedding.h @@ -101,7 +101,7 @@ class Embedding : public Operator { const double /*norm_type*/ = 2.0, const bool /*scale_grad_by_freq*/ = false, const bool /*sparse*/ = false) { - auto out_shape = input.shape(); + typename TensorLike::Shape out_shape{input.shape()}; out_shape.push_back(weight.size(1)); return TensorLike::Empty(out_shape, weight.dtype(), weight.device()); diff --git a/src/base/flash_attn_with_kvcache.h b/src/base/flash_attn_with_kvcache.h index b775e2d73..2c6144a85 100644 --- a/src/base/flash_attn_with_kvcache.h +++ b/src/base/flash_attn_with_kvcache.h @@ -90,46 +90,59 @@ class FlashAttnWithKvcache : public Operator { : q_shape_{q.shape()}, k_cache_shape_{k_cache.shape()}, v_cache_shape_{v_cache.shape()}, - k_shape_{k.has_value() ? k->shape() : Tensor::Shape{}}, - v_shape_{v.has_value() ? v->shape() : Tensor::Shape{}}, - rotary_cos_shape_{rotary_cos.has_value() ? rotary_cos->shape() - : Tensor::Shape{}}, - rotary_sin_shape_{rotary_sin.has_value() ? rotary_sin->shape() - : Tensor::Shape{}}, - cache_seqlens_shape_{cache_seqlens.has_value() ? cache_seqlens->shape() - : Tensor::Shape{}}, + k_shape_{k.has_value() ? Tensor::Shape{k->shape()} : Tensor::Shape{}}, + v_shape_{v.has_value() ? Tensor::Shape{v->shape()} : Tensor::Shape{}}, + rotary_cos_shape_{rotary_cos.has_value() + ? Tensor::Shape{rotary_cos->shape()} + : Tensor::Shape{}}, + rotary_sin_shape_{rotary_sin.has_value() + ? Tensor::Shape{rotary_sin->shape()} + : Tensor::Shape{}}, + cache_seqlens_shape_{cache_seqlens.has_value() + ? Tensor::Shape{cache_seqlens->shape()} + : Tensor::Shape{}}, cache_batch_idx_shape_{cache_batch_idx.has_value() - ? cache_batch_idx->shape() + ? Tensor::Shape{cache_batch_idx->shape()} : Tensor::Shape{}}, - cache_leftpad_shape_{cache_leftpad.has_value() ? cache_leftpad->shape() - : Tensor::Shape{}}, - block_table_shape_{block_table.has_value() ? block_table->shape() - : Tensor::Shape{}}, - alibi_slopes_shape_{alibi_slopes.has_value() ? alibi_slopes->shape() - : Tensor::Shape{}}, + cache_leftpad_shape_{cache_leftpad.has_value() + ? Tensor::Shape{cache_leftpad->shape()} + : Tensor::Shape{}}, + block_table_shape_{block_table.has_value() + ? Tensor::Shape{block_table->shape()} + : Tensor::Shape{}}, + alibi_slopes_shape_{alibi_slopes.has_value() + ? Tensor::Shape{alibi_slopes->shape()} + : Tensor::Shape{}}, out_shape_{out.shape()}, q_strides_{q.strides()}, k_cache_strides_{k_cache.strides()}, v_cache_strides_{v_cache.strides()}, - k_strides_{k.has_value() ? k->strides() : Tensor::Strides{}}, - v_strides_{v.has_value() ? v->strides() : Tensor::Strides{}}, - rotary_cos_strides_{rotary_cos.has_value() ? rotary_cos->strides() - : Tensor::Strides{}}, - rotary_sin_strides_{rotary_sin.has_value() ? rotary_sin->strides() - : Tensor::Strides{}}, + k_strides_{k.has_value() ? Tensor::Strides{k->strides()} + : Tensor::Strides{}}, + v_strides_{v.has_value() ? Tensor::Strides{v->strides()} + : Tensor::Strides{}}, + rotary_cos_strides_{rotary_cos.has_value() + ? Tensor::Strides{rotary_cos->strides()} + : Tensor::Strides{}}, + rotary_sin_strides_{rotary_sin.has_value() + ? Tensor::Strides{rotary_sin->strides()} + : Tensor::Strides{}}, cache_seqlens_strides_{cache_seqlens.has_value() - ? cache_seqlens->strides() + ? Tensor::Strides{cache_seqlens->strides()} : Tensor::Strides{}}, - cache_batch_idx_strides_{cache_batch_idx.has_value() - ? cache_batch_idx->strides() - : Tensor::Strides{}}, + cache_batch_idx_strides_{ + cache_batch_idx.has_value() + ? Tensor::Strides{cache_batch_idx->strides()} + : Tensor::Strides{}}, cache_leftpad_strides_{cache_leftpad.has_value() - ? cache_leftpad->strides() + ? Tensor::Strides{cache_leftpad->strides()} : Tensor::Strides{}}, - block_table_strides_{block_table.has_value() ? block_table->strides() - : Tensor::Strides{}}, - alibi_slopes_strides_{alibi_slopes.has_value() ? alibi_slopes->strides() - : Tensor::Strides{}}, + block_table_strides_{block_table.has_value() + ? Tensor::Strides{block_table->strides()} + : Tensor::Strides{}}, + alibi_slopes_strides_{alibi_slopes.has_value() + ? Tensor::Strides{alibi_slopes->strides()} + : Tensor::Strides{}}, out_strides_{out.strides()}, q_dtype_{q.dtype()}, k_cache_dtype_{k_cache.dtype()}, diff --git a/src/base/histogram.h b/src/base/histogram.h index 787c459c6..4181646c2 100644 --- a/src/base/histogram.h +++ b/src/base/histogram.h @@ -26,8 +26,10 @@ class Histogram : public Operator { bin_edges_strides_{bin_edges.strides()}, bin_edges_type_{bin_edges.dtype()}, has_weight_{weight.has_value()}, - weight_shape_{weight ? weight->shape() : Tensor::Shape{}}, - weight_strides_{weight ? weight->strides() : Tensor::Strides{}}, + weight_shape_{weight ? Tensor::Shape{weight->shape()} + : Tensor::Shape{}}, + weight_strides_{weight ? Tensor::Strides{weight->strides()} + : Tensor::Strides{}}, weight_type_{weight ? weight->dtype() : DataType::kFloat32}, density_{density}, device_index_{hist.device().index()} {} @@ -46,8 +48,10 @@ class Histogram : public Operator { bin_edges_strides_{bin_edges.strides()}, bin_edges_type_{bin_edges.dtype()}, has_weight_{weight.has_value()}, - weight_shape_{weight ? weight->shape() : Tensor::Shape{}}, - weight_strides_{weight ? weight->strides() : Tensor::Strides{}}, + weight_shape_{weight ? Tensor::Shape{weight->shape()} + : Tensor::Shape{}}, + weight_strides_{weight ? Tensor::Strides{weight->strides()} + : Tensor::Strides{}}, weight_type_{weight ? weight->dtype() : DataType::kFloat32}, density_{density}, bins_{bins}, diff --git a/src/base/internal_batch_norm_with_update.h b/src/base/internal_batch_norm_with_update.h index 4a59647b0..b1288c3e8 100644 --- a/src/base/internal_batch_norm_with_update.h +++ b/src/base/internal_batch_norm_with_update.h @@ -36,12 +36,15 @@ class BatchNormWithUpdate : public Operator { reserve_strides_{reserve.strides()}, reserve_type_{reserve.dtype()}, has_weight_{weight.has_value()}, - weight_shape_{weight ? weight->shape() : Tensor::Shape{}}, - weight_strides_{weight ? weight->strides() : Tensor::Strides{}}, + weight_shape_{weight ? Tensor::Shape{weight->shape()} + : Tensor::Shape{}}, + weight_strides_{weight ? Tensor::Strides{weight->strides()} + : Tensor::Strides{}}, weight_type_{weight ? weight->dtype() : DataType::kFloat32}, has_bias_{bias.has_value()}, - bias_shape_{bias ? bias->shape() : Tensor::Shape{}}, - bias_strides_{bias ? bias->strides() : Tensor::Strides{}}, + bias_shape_{bias ? Tensor::Shape{bias->shape()} : Tensor::Shape{}}, + bias_strides_{bias ? Tensor::Strides{bias->strides()} + : Tensor::Strides{}}, bias_type_{bias ? bias->dtype() : DataType::kFloat32}, momentum_{momentum}, eps_{eps}, diff --git a/src/base/internal_conv_depthwise2d.h b/src/base/internal_conv_depthwise2d.h index 8f61092b9..291dae48c 100644 --- a/src/base/internal_conv_depthwise2d.h +++ b/src/base/internal_conv_depthwise2d.h @@ -26,8 +26,9 @@ class ConvDepthwise2d : public Operator { out_strides_{out.strides()}, out_type_{out.dtype()}, has_bias_{bias.has_value()}, - bias_shape_{bias ? bias->shape() : Tensor::Shape{}}, - bias_strides_{bias ? bias->strides() : Tensor::Strides{}}, + bias_shape_{bias ? Tensor::Shape{bias->shape()} : Tensor::Shape{}}, + bias_strides_{bias ? Tensor::Strides{bias->strides()} + : Tensor::Strides{}}, bias_type_{bias ? bias->dtype() : DataType::kFloat32}, kernel_size_{kernel_size}, stride_{stride}, diff --git a/src/base/internal_native_batch_norm_legit.h b/src/base/internal_native_batch_norm_legit.h index d3aa4c309..4da0da085 100644 --- a/src/base/internal_native_batch_norm_legit.h +++ b/src/base/internal_native_batch_norm_legit.h @@ -33,12 +33,15 @@ class NativeBatchNormLegit : public Operator { save_invstd_strides_{save_invstd.strides()}, save_invstd_type_{save_invstd.dtype()}, has_weight_{weight.has_value()}, - weight_shape_{weight ? weight->shape() : Tensor::Shape{}}, - weight_strides_{weight ? weight->strides() : Tensor::Strides{}}, + weight_shape_{weight ? Tensor::Shape{weight->shape()} + : Tensor::Shape{}}, + weight_strides_{weight ? Tensor::Strides{weight->strides()} + : Tensor::Strides{}}, weight_type_{weight ? weight->dtype() : DataType::kFloat32}, has_bias_{bias.has_value()}, - bias_shape_{bias ? bias->shape() : Tensor::Shape{}}, - bias_strides_{bias ? bias->strides() : Tensor::Strides{}}, + bias_shape_{bias ? Tensor::Shape{bias->shape()} : Tensor::Shape{}}, + bias_strides_{bias ? Tensor::Strides{bias->strides()} + : Tensor::Strides{}}, bias_type_{bias ? bias->dtype() : DataType::kFloat32}, training_{training}, momentum_{momentum}, @@ -62,12 +65,15 @@ class NativeBatchNormLegit : public Operator { save_invstd_strides_{save_invstd.strides()}, save_invstd_type_{save_invstd.dtype()}, has_weight_{weight.has_value()}, - weight_shape_{weight ? weight->shape() : Tensor::Shape{}}, - weight_strides_{weight ? weight->strides() : Tensor::Strides{}}, + weight_shape_{weight ? Tensor::Shape{weight->shape()} + : Tensor::Shape{}}, + weight_strides_{weight ? Tensor::Strides{weight->strides()} + : Tensor::Strides{}}, weight_type_{weight ? weight->dtype() : DataType::kFloat32}, has_bias_{bias.has_value()}, - bias_shape_{bias ? bias->shape() : Tensor::Shape{}}, - bias_strides_{bias ? bias->strides() : Tensor::Strides{}}, + bias_shape_{bias ? Tensor::Shape{bias->shape()} : Tensor::Shape{}}, + bias_strides_{bias ? Tensor::Strides{bias->strides()} + : Tensor::Strides{}}, bias_type_{bias ? bias->dtype() : DataType::kFloat32}, training_{training}, momentum_{momentum}, diff --git a/src/base/internal_slow_conv2d_forward.h b/src/base/internal_slow_conv2d_forward.h index 8c60f4137..1a77c565d 100644 --- a/src/base/internal_slow_conv2d_forward.h +++ b/src/base/internal_slow_conv2d_forward.h @@ -25,8 +25,9 @@ class SlowConv2dForward : public Operator { output_strides_{output.strides()}, output_type_{output.dtype()}, has_bias_{bias.has_value()}, - bias_shape_{bias ? bias->shape() : Tensor::Shape{}}, - bias_strides_{bias ? bias->strides() : Tensor::Strides{}}, + bias_shape_{bias ? Tensor::Shape{bias->shape()} : Tensor::Shape{}}, + bias_strides_{bias ? Tensor::Strides{bias->strides()} + : Tensor::Strides{}}, bias_type_{bias ? bias->dtype() : DataType::kFloat32}, kernel_size_{kernel_size}, stride_{stride}, diff --git a/src/base/linalg_matrix_rank.h b/src/base/linalg_matrix_rank.h index 4e29d230b..c34d51edf 100644 --- a/src/base/linalg_matrix_rank.h +++ b/src/base/linalg_matrix_rank.h @@ -30,12 +30,14 @@ class MatrixRank : public Operator { out_strides_{out.strides()}, out_type_{out.dtype()}, has_atol_{atol.has_value()}, - atol_shape_{atol ? atol->shape() : Tensor::Shape{}}, - atol_strides_{atol ? atol->strides() : Tensor::Strides{}}, + atol_shape_{atol ? Tensor::Shape{atol->shape()} : Tensor::Shape{}}, + atol_strides_{atol ? Tensor::Strides{atol->strides()} + : Tensor::Strides{}}, atol_type_{atol ? atol->dtype() : DataType::kFloat32}, has_rtol_{rtol.has_value()}, - rtol_shape_{rtol ? rtol->shape() : Tensor::Shape{}}, - rtol_strides_{rtol ? rtol->strides() : Tensor::Strides{}}, + rtol_shape_{rtol ? Tensor::Shape{rtol->shape()} : Tensor::Shape{}}, + rtol_strides_{rtol ? Tensor::Strides{rtol->strides()} + : Tensor::Strides{}}, rtol_type_{rtol ? rtol->dtype() : DataType::kFloat32}, hermitian_{hermitian}, device_index_{out.device().index()} {} diff --git a/src/base/linalg_pinv.h b/src/base/linalg_pinv.h index 3d84d0e86..0b45e4a4a 100644 --- a/src/base/linalg_pinv.h +++ b/src/base/linalg_pinv.h @@ -29,12 +29,14 @@ class Pinv : public Operator { out_strides_{out.strides()}, out_type_{out.dtype()}, has_atol_{atol.has_value()}, - atol_shape_{atol ? atol->shape() : Tensor::Shape{}}, - atol_strides_{atol ? atol->strides() : Tensor::Strides{}}, + atol_shape_{atol ? Tensor::Shape{atol->shape()} : Tensor::Shape{}}, + atol_strides_{atol ? Tensor::Strides{atol->strides()} + : Tensor::Strides{}}, atol_type_{atol ? atol->dtype() : DataType::kFloat32}, has_rtol_{rtol.has_value()}, - rtol_shape_{rtol ? rtol->shape() : Tensor::Shape{}}, - rtol_strides_{rtol ? rtol->strides() : Tensor::Strides{}}, + rtol_shape_{rtol ? Tensor::Shape{rtol->shape()} : Tensor::Shape{}}, + rtol_strides_{rtol ? Tensor::Strides{rtol->strides()} + : Tensor::Strides{}}, rtol_type_{rtol ? rtol->dtype() : DataType::kFloat32}, hermitian_{hermitian}, device_index_{out.device().index()} {} diff --git a/src/base/moe_sum.h b/src/base/moe_sum.h index d5b5259de..bb2eba5ce 100644 --- a/src/base/moe_sum.h +++ b/src/base/moe_sum.h @@ -60,8 +60,7 @@ class MoeSum : public Operator { (hidden_size_ == 0 || num_tokens_ <= kMaxSignedIndex / hidden_size_) && "`MoeSum` output size must fit signed index arithmetic"); - const auto offsets_fit = [](const Tensor::Shape& shape, - const Tensor::Strides& strides) { + const auto offsets_fit = [](const auto& shape, const auto& strides) { uint64_t max_offset = 0; constexpr auto kMaxOffset = static_cast(std::numeric_limits::max()); diff --git a/src/base/multi_margin_loss.h b/src/base/multi_margin_loss.h index 984ac13a6..f51b14394 100644 --- a/src/base/multi_margin_loss.h +++ b/src/base/multi_margin_loss.h @@ -22,8 +22,10 @@ class MultiMarginLoss : public Operator { out_strides_{out.strides()}, out_type_{out.dtype()}, has_weight_{weight.has_value()}, - weight_shape_{weight ? weight->shape() : Tensor::Shape{}}, - weight_strides_{weight ? weight->strides() : Tensor::Strides{}}, + weight_shape_{weight ? Tensor::Shape{weight->shape()} + : Tensor::Shape{}}, + weight_strides_{weight ? Tensor::Strides{weight->strides()} + : Tensor::Strides{}}, weight_type_{weight ? weight->dtype() : DataType::kFloat32}, p_{p}, margin_{margin}, diff --git a/src/base/multi_margin_loss_backward.h b/src/base/multi_margin_loss_backward.h index ad05e9820..26209699a 100644 --- a/src/base/multi_margin_loss_backward.h +++ b/src/base/multi_margin_loss_backward.h @@ -27,8 +27,10 @@ class MultiMarginLossBackward : public Operator { grad_input_strides_{grad_input.strides()}, grad_input_type_{grad_input.dtype()}, has_weight_{weight.has_value()}, - weight_shape_{weight ? weight->shape() : Tensor::Shape{}}, - weight_strides_{weight ? weight->strides() : Tensor::Strides{}}, + weight_shape_{weight ? Tensor::Shape{weight->shape()} + : Tensor::Shape{}}, + weight_strides_{weight ? Tensor::Strides{weight->strides()} + : Tensor::Strides{}}, weight_type_{weight ? weight->dtype() : DataType::kFloat32}, p_{p}, margin_{margin}, diff --git a/src/base/native_batch_norm.h b/src/base/native_batch_norm.h index f88f2e5fd..0c69ec9d5 100644 --- a/src/base/native_batch_norm.h +++ b/src/base/native_batch_norm.h @@ -28,25 +28,30 @@ class NativeBatchNorm : public Operator { save_invstd_strides_{save_invstd.strides()}, save_invstd_type_{save_invstd.dtype()}, has_weight_{weight.has_value()}, - weight_shape_{weight ? weight->shape() : Tensor::Shape{}}, - weight_strides_{weight ? weight->strides() : Tensor::Strides{}}, + weight_shape_{weight ? Tensor::Shape{weight->shape()} + : Tensor::Shape{}}, + weight_strides_{weight ? Tensor::Strides{weight->strides()} + : Tensor::Strides{}}, weight_type_{weight ? weight->dtype() : DataType::kFloat32}, has_bias_{bias.has_value()}, - bias_shape_{bias ? bias->shape() : Tensor::Shape{}}, - bias_strides_{bias ? bias->strides() : Tensor::Strides{}}, + bias_shape_{bias ? Tensor::Shape{bias->shape()} : Tensor::Shape{}}, + bias_strides_{bias ? Tensor::Strides{bias->strides()} + : Tensor::Strides{}}, bias_type_{bias ? bias->dtype() : DataType::kFloat32}, has_running_mean_{running_mean.has_value()}, - running_mean_shape_{running_mean ? running_mean->shape() + running_mean_shape_{running_mean ? Tensor::Shape{running_mean->shape()} : Tensor::Shape{}}, - running_mean_strides_{running_mean ? running_mean->strides() - : Tensor::Strides{}}, + running_mean_strides_{running_mean + ? Tensor::Strides{running_mean->strides()} + : Tensor::Strides{}}, running_mean_type_{running_mean ? running_mean->dtype() : DataType::kFloat32}, has_running_var_{running_var.has_value()}, - running_var_shape_{running_var ? running_var->shape() + running_var_shape_{running_var ? Tensor::Shape{running_var->shape()} : Tensor::Shape{}}, - running_var_strides_{running_var ? running_var->strides() - : Tensor::Strides{}}, + running_var_strides_{running_var + ? Tensor::Strides{running_var->strides()} + : Tensor::Strides{}}, running_var_type_{running_var ? running_var->dtype() : DataType::kFloat32}, training_{training}, diff --git a/src/base/nll_loss.h b/src/base/nll_loss.h index b5dfc5c56..8ceb8fb96 100644 --- a/src/base/nll_loss.h +++ b/src/base/nll_loss.h @@ -25,8 +25,10 @@ class NllLoss : public Operator { out_strides_{out.strides()}, out_type_{out.dtype()}, has_weight_{weight.has_value()}, - weight_shape_{weight ? weight->shape() : Tensor::Shape{}}, - weight_strides_{weight ? weight->strides() : Tensor::Strides{}}, + weight_shape_{weight ? Tensor::Shape{weight->shape()} + : Tensor::Shape{}}, + weight_strides_{weight ? Tensor::Strides{weight->strides()} + : Tensor::Strides{}}, weight_type_{weight ? weight->dtype() : DataType::kFloat32}, reduction_{ ReductionFromPythonArguments(size_average, reduce, reduction)}, @@ -48,8 +50,10 @@ class NllLoss : public Operator { out_strides_{out.strides()}, out_type_{out.dtype()}, has_weight_{weight.has_value()}, - weight_shape_{weight ? weight->shape() : Tensor::Shape{}}, - weight_strides_{weight ? weight->strides() : Tensor::Strides{}}, + weight_shape_{weight ? Tensor::Shape{weight->shape()} + : Tensor::Shape{}}, + weight_strides_{weight ? Tensor::Strides{weight->strides()} + : Tensor::Strides{}}, weight_type_{weight ? weight->dtype() : DataType::kFloat32}, reduction_{reduction}, ignore_index_{ignore_index}, diff --git a/src/base/nll_loss2d.h b/src/base/nll_loss2d.h index 3c144b157..591199493 100644 --- a/src/base/nll_loss2d.h +++ b/src/base/nll_loss2d.h @@ -22,8 +22,10 @@ class NllLoss2d : public Operator { out_strides_{out.strides()}, out_type_{out.dtype()}, has_weight_{weight.has_value()}, - weight_shape_{weight ? weight->shape() : Tensor::Shape{}}, - weight_strides_{weight ? weight->strides() : Tensor::Strides{}}, + weight_shape_{weight ? Tensor::Shape{weight->shape()} + : Tensor::Shape{}}, + weight_strides_{weight ? Tensor::Strides{weight->strides()} + : Tensor::Strides{}}, weight_type_{weight ? weight->dtype() : DataType::kFloat32}, reduction_{reduction}, ignore_index_{ignore_index}, diff --git a/src/base/nll_loss2d_backward.h b/src/base/nll_loss2d_backward.h index 704b207ec..81527295a 100644 --- a/src/base/nll_loss2d_backward.h +++ b/src/base/nll_loss2d_backward.h @@ -29,8 +29,10 @@ class NllLoss2dBackward : public Operator { grad_input_strides_{grad_input.strides()}, grad_input_type_{grad_input.dtype()}, has_weight_{weight.has_value()}, - weight_shape_{weight ? weight->shape() : Tensor::Shape{}}, - weight_strides_{weight ? weight->strides() : Tensor::Strides{}}, + weight_shape_{weight ? Tensor::Shape{weight->shape()} + : Tensor::Shape{}}, + weight_strides_{weight ? Tensor::Strides{weight->strides()} + : Tensor::Strides{}}, weight_type_{weight ? weight->dtype() : DataType::kFloat32}, reduction_{reduction}, ignore_index_{ignore_index}, diff --git a/src/base/nll_loss2d_forward.h b/src/base/nll_loss2d_forward.h index 82730c03e..696030ea1 100644 --- a/src/base/nll_loss2d_forward.h +++ b/src/base/nll_loss2d_forward.h @@ -26,8 +26,10 @@ class NllLoss2dForward : public Operator { total_weight_strides_{total_weight.strides()}, total_weight_type_{total_weight.dtype()}, has_weight_{weight.has_value()}, - weight_shape_{weight ? weight->shape() : Tensor::Shape{}}, - weight_strides_{weight ? weight->strides() : Tensor::Strides{}}, + weight_shape_{weight ? Tensor::Shape{weight->shape()} + : Tensor::Shape{}}, + weight_strides_{weight ? Tensor::Strides{weight->strides()} + : Tensor::Strides{}}, weight_type_{weight ? weight->dtype() : DataType::kFloat32}, reduction_{reduction}, ignore_index_{ignore_index}, diff --git a/src/base/nll_loss_backward.h b/src/base/nll_loss_backward.h index 067f91900..7ee3a8290 100644 --- a/src/base/nll_loss_backward.h +++ b/src/base/nll_loss_backward.h @@ -29,8 +29,10 @@ class NllLossBackward : public Operator { grad_input_strides_{grad_input.strides()}, grad_input_type_{grad_input.dtype()}, has_weight_{weight.has_value()}, - weight_shape_{weight ? weight->shape() : Tensor::Shape{}}, - weight_strides_{weight ? weight->strides() : Tensor::Strides{}}, + weight_shape_{weight ? Tensor::Shape{weight->shape()} + : Tensor::Shape{}}, + weight_strides_{weight ? Tensor::Strides{weight->strides()} + : Tensor::Strides{}}, weight_type_{weight ? weight->dtype() : DataType::kFloat32}, reduction_{reduction}, ignore_index_{ignore_index}, diff --git a/src/base/nll_loss_forward.h b/src/base/nll_loss_forward.h index 17397662b..30119cc16 100644 --- a/src/base/nll_loss_forward.h +++ b/src/base/nll_loss_forward.h @@ -25,8 +25,10 @@ class NllLossForward : public Operator { total_weight_strides_{total_weight.strides()}, total_weight_type_{total_weight.dtype()}, has_weight_{weight.has_value()}, - weight_shape_{weight ? weight->shape() : Tensor::Shape{}}, - weight_strides_{weight ? weight->strides() : Tensor::Strides{}}, + weight_shape_{weight ? Tensor::Shape{weight->shape()} + : Tensor::Shape{}}, + weight_strides_{weight ? Tensor::Strides{weight->strides()} + : Tensor::Strides{}}, weight_type_{weight ? weight->dtype() : DataType::kFloat32}, reduction_{reduction}, ignore_index_{ignore_index}, diff --git a/src/base/rotary_embedding.h b/src/base/rotary_embedding.h index f405faa43..01073bf6f 100644 --- a/src/base/rotary_embedding.h +++ b/src/base/rotary_embedding.h @@ -16,11 +16,13 @@ class RotaryEmbedding : public Operator { int64_t rope_dim_offset = 0, bool inverse = false) : positions_shape_{positions.shape()}, query_shape_{query.shape()}, - key_shape_{key.has_value() ? key->shape() : Tensor::Shape{}}, + key_shape_{key.has_value() ? Tensor::Shape{key->shape()} + : Tensor::Shape{}}, cos_sin_cache_shape_{cos_sin_cache.shape()}, positions_strides_{positions.strides()}, query_strides_{query.strides()}, - key_strides_{key.has_value() ? key->strides() : Tensor::Strides{}}, + key_strides_{key.has_value() ? Tensor::Strides{key->strides()} + : Tensor::Strides{}}, cos_sin_cache_strides_{cos_sin_cache.strides()}, positions_type_{positions.dtype()}, query_type_{query.dtype()}, diff --git a/src/base/scaled_dot_product_attention.h b/src/base/scaled_dot_product_attention.h index ace5d5cde..40d60f65d 100644 --- a/src/base/scaled_dot_product_attention.h +++ b/src/base/scaled_dot_product_attention.h @@ -18,14 +18,16 @@ class ScaledDotProductAttention : public Operator { : query_shape_{query.shape()}, key_shape_{key.shape()}, value_shape_{value.shape()}, - attn_mask_shape_{attn_mask.has_value() ? attn_mask->shape() - : Tensor::Shape{}}, + attn_mask_shape_{attn_mask.has_value() + ? Tensor::Shape{attn_mask->shape()} + : Tensor::Shape{}}, out_shape_{out.shape()}, query_strides_{query.strides()}, key_strides_{key.strides()}, value_strides_{value.strides()}, - attn_mask_strides_{attn_mask.has_value() ? attn_mask->strides() - : Tensor::Strides{}}, + attn_mask_strides_{attn_mask.has_value() + ? Tensor::Strides{attn_mask->strides()} + : Tensor::Strides{}}, out_strides_{out.strides()}, query_type_{query.dtype()}, attn_mask_type_{attn_mask.has_value() ? attn_mask->dtype() @@ -43,7 +45,7 @@ class ScaledDotProductAttention : public Operator { "dtypes"); assert(query.size(-1) == key.size(-1) && key.size(-2) == value.size(-2) && "`ScaledDotProductAttention` input dimensions are incompatible"); - auto expected_out_shape = query.shape(); + Tensor::Shape expected_out_shape{query.shape()}; expected_out_shape.back() = value.size(-1); assert(out.shape() == expected_out_shape && "`ScaledDotProductAttention` output shape is incorrect"); @@ -83,7 +85,7 @@ class ScaledDotProductAttention : public Operator { (void)scale; (void)enable_gqa; - auto out_shape = query.shape(); + typename TensorLike::Shape out_shape{query.shape()}; out_shape.back() = value.size(-1); return TensorLike::Empty(out_shape, query.dtype(), query.device()); } diff --git a/src/base/searchsorted.h b/src/base/searchsorted.h index 481811946..f76575260 100644 --- a/src/base/searchsorted.h +++ b/src/base/searchsorted.h @@ -24,8 +24,10 @@ class Searchsorted : public Operator { out_strides_{out.strides()}, out_type_{out.dtype()}, has_sorter_{sorter.has_value()}, - sorter_shape_{sorter ? sorter->shape() : Tensor::Shape{}}, - sorter_strides_{sorter ? sorter->strides() : Tensor::Strides{}}, + sorter_shape_{sorter ? Tensor::Shape{sorter->shape()} + : Tensor::Shape{}}, + sorter_strides_{sorter ? Tensor::Strides{sorter->strides()} + : Tensor::Strides{}}, sorter_type_{sorter ? sorter->dtype() : DataType::kFloat32}, out_int32_{out_int32}, right_{right}, @@ -43,8 +45,10 @@ class Searchsorted : public Operator { out_strides_{out.strides()}, out_type_{out.dtype()}, has_sorter_{sorter.has_value()}, - sorter_shape_{sorter ? sorter->shape() : Tensor::Shape{}}, - sorter_strides_{sorter ? sorter->strides() : Tensor::Strides{}}, + sorter_shape_{sorter ? Tensor::Shape{sorter->shape()} + : Tensor::Shape{}}, + sorter_strides_{sorter ? Tensor::Strides{sorter->strides()} + : Tensor::Strides{}}, sorter_type_{sorter ? sorter->dtype() : DataType::kFloat32}, out_int32_{out_int32}, right_{right}, diff --git a/src/base/silu_and_mul.h b/src/base/silu_and_mul.h index 603c132d2..19a60c115 100644 --- a/src/base/silu_and_mul.h +++ b/src/base/silu_and_mul.h @@ -37,7 +37,7 @@ class SiluAndMul : public Operator { template static auto MakeReturnValue(const TensorLike& input) { - auto out_shape = input.shape(); + typename TensorLike::Shape out_shape{input.shape()}; assert(!out_shape.empty() && out_shape.back() % 2 == 0 && "`SiluAndMul` requires an even input last dimension"); out_shape.back() /= 2; diff --git a/src/base/slow_conv3d.h b/src/base/slow_conv3d.h index 3f55f8425..a33025f9f 100644 --- a/src/base/slow_conv3d.h +++ b/src/base/slow_conv3d.h @@ -25,8 +25,9 @@ class SlowConv3d : public Operator { out_strides_{out.strides()}, out_type_{out.dtype()}, has_bias_{bias.has_value()}, - bias_shape_{bias ? bias->shape() : Tensor::Shape{}}, - bias_strides_{bias ? bias->strides() : Tensor::Strides{}}, + bias_shape_{bias ? Tensor::Shape{bias->shape()} : Tensor::Shape{}}, + bias_strides_{bias ? Tensor::Strides{bias->strides()} + : Tensor::Strides{}}, bias_type_{bias ? bias->dtype() : DataType::kFloat32}, kernel_size_{kernel_size}, stride_{stride}, diff --git a/src/base/slow_conv3d_forward.h b/src/base/slow_conv3d_forward.h index 75a42d082..0786cd8c0 100644 --- a/src/base/slow_conv3d_forward.h +++ b/src/base/slow_conv3d_forward.h @@ -25,8 +25,9 @@ class SlowConv3dForward : public Operator { output_strides_{output.strides()}, output_type_{output.dtype()}, has_bias_{bias.has_value()}, - bias_shape_{bias ? bias->shape() : Tensor::Shape{}}, - bias_strides_{bias ? bias->strides() : Tensor::Strides{}}, + bias_shape_{bias ? Tensor::Shape{bias->shape()} : Tensor::Shape{}}, + bias_strides_{bias ? Tensor::Strides{bias->strides()} + : Tensor::Strides{}}, bias_type_{bias ? bias->dtype() : DataType::kFloat32}, kernel_size_{kernel_size}, stride_{stride}, diff --git a/src/base/slow_conv_transpose2d.h b/src/base/slow_conv_transpose2d.h index cc561cba3..59ffc8293 100644 --- a/src/base/slow_conv_transpose2d.h +++ b/src/base/slow_conv_transpose2d.h @@ -27,8 +27,9 @@ class SlowConvTranspose2d : public Operator { out_strides_{out.strides()}, out_type_{out.dtype()}, has_bias_{bias.has_value()}, - bias_shape_{bias ? bias->shape() : Tensor::Shape{}}, - bias_strides_{bias ? bias->strides() : Tensor::Strides{}}, + bias_shape_{bias ? Tensor::Shape{bias->shape()} : Tensor::Shape{}}, + bias_strides_{bias ? Tensor::Strides{bias->strides()} + : Tensor::Strides{}}, bias_type_{bias ? bias->dtype() : DataType::kFloat32}, kernel_size_{kernel_size}, stride_{stride}, diff --git a/src/base/slow_conv_transpose3d.h b/src/base/slow_conv_transpose3d.h index f0ce6dfe6..209bca8bf 100644 --- a/src/base/slow_conv_transpose3d.h +++ b/src/base/slow_conv_transpose3d.h @@ -27,8 +27,9 @@ class SlowConvTranspose3d : public Operator { out_strides_{out.strides()}, out_type_{out.dtype()}, has_bias_{bias.has_value()}, - bias_shape_{bias ? bias->shape() : Tensor::Shape{}}, - bias_strides_{bias ? bias->strides() : Tensor::Strides{}}, + bias_shape_{bias ? Tensor::Shape{bias->shape()} : Tensor::Shape{}}, + bias_strides_{bias ? Tensor::Strides{bias->strides()} + : Tensor::Strides{}}, bias_type_{bias ? bias->dtype() : DataType::kFloat32}, kernel_size_{kernel_size}, stride_{stride}, diff --git a/src/base/thnn_conv2d.h b/src/base/thnn_conv2d.h index 810a799f1..f56bc22f5 100644 --- a/src/base/thnn_conv2d.h +++ b/src/base/thnn_conv2d.h @@ -25,8 +25,9 @@ class ThnnConv2d : public Operator { out_strides_{out.strides()}, out_type_{out.dtype()}, has_bias_{bias.has_value()}, - bias_shape_{bias ? bias->shape() : Tensor::Shape{}}, - bias_strides_{bias ? bias->strides() : Tensor::Strides{}}, + bias_shape_{bias ? Tensor::Shape{bias->shape()} : Tensor::Shape{}}, + bias_strides_{bias ? Tensor::Strides{bias->strides()} + : Tensor::Strides{}}, bias_type_{bias ? bias->dtype() : DataType::kFloat32}, kernel_size_{kernel_size}, stride_{stride}, diff --git a/src/pybind11_utils.h b/src/pybind11_utils.h index 292a2c00b..fbdf4b61e 100644 --- a/src/pybind11_utils.h +++ b/src/pybind11_utils.h @@ -143,17 +143,31 @@ inline Device DeviceFromPybind11HandleImpl(py::handle obj) { return Device{DeviceTypeFromString(device_type_str), device_index}; } +template +Metadata MetadataFromPybind11Sequence(py::handle obj) { + auto values{py::reinterpret_borrow(obj)}; + Metadata metadata; + metadata.reserve(values.size()); + + for (const auto& value : values) { + metadata.push_back(value.cast()); + } + + return metadata; +} + inline Tensor TensorFromPybind11HandleImpl(py::handle obj) { auto data{ reinterpret_cast(obj.attr("data_ptr")().cast())}; - auto shape{obj.attr("shape").cast()}; + auto shape{MetadataFromPybind11Sequence(obj.attr("shape"))}; auto dtype{DataTypeFromPybind11HandleImpl(obj.attr("dtype"))}; auto device{DeviceFromPybind11HandleImpl(obj)}; - auto strides{obj.attr("stride")().cast()}; + auto strides{ + MetadataFromPybind11Sequence(obj.attr("stride")())}; return Tensor{data, std::move(shape), dtype, device, std::move(strides)}; } diff --git a/src/torch/tensor_.h b/src/torch/tensor_.h index e199bf704..556eed5cd 100644 --- a/src/torch/tensor_.h +++ b/src/torch/tensor_.h @@ -5,6 +5,7 @@ #include #include +#include #include "tensor.h" #include "torch/device_.h" @@ -83,9 +84,9 @@ inline at::ScalarType ToAtenDataType(DataType dtype) { // Build an ATen tensor from explicit metadata. Use this instead of reading // shape/strides from the `Tensor` parameter, which may have been moved-from // by the `Call()` dispatch path (see `operator.h`). -template +template inline at::Tensor ToAtenTensor( - void* data, const Tensor::Shape& shape, const Tensor::Strides& strides, + void* data, const ShapeRange& shape, const StridesRange& strides, DataType dtype, int device_index = 0, std::optional dtype_override = std::nullopt) { std::vector at_shape(shape.begin(), shape.end()); diff --git a/tests/test_cpp_api.py b/tests/test_cpp_api.py index 02933b8bf..1cc60fe34 100644 --- a/tests/test_cpp_api.py +++ b/tests/test_cpp_api.py @@ -58,6 +58,33 @@ def test_cpp_returning_call_smoke(tmp_path): _run([str(binary)]) +@pytest.mark.parametrize( + "header", + ( + "base/clamp.h", + "base/moe_sum.h", + "base/scaled_dot_product_attention.h", + ), +) +def test_cpp_base_headers_compile_with_metadata_views(tmp_path, header): + install_prefix = _install_prefix() + include_dir = install_prefix / "include" + source = tmp_path / f"{Path(header).stem}_metadata_view.cc" + source.write_text(f"#include <{header}>\n\nint main() {{ return 0; }}\n") + + _run( + [ + _compiler("CXX", "c++"), + "-std=c++17", + "-Werror", + "-UNDEBUG", + "-fsyntax-only", + f"-I{include_dir}", + str(source), + ] + ) + + def _install_prefix(): prefix = os.environ.get("INFINI_OPS_INSTALL_PREFIX") @@ -146,10 +173,15 @@ def _run(command): r""" #include + #include + #include + #include + #include #include #include #include + #include #include #include @@ -179,9 +211,32 @@ class OwningTensor { const void* data() const { return data_.data(); } - const Shape& shape() const { return shape_; } + template ().shape())> + decltype(auto) shape() const { + if constexpr (std::is_reference_v) { + return (shape_); + } else { + return ShapeAccess{shape_.data(), shape_.size()}; + } + } + + template ().strides())> + decltype(auto) strides() const { + if constexpr (std::is_reference_v) { + return (strides_); + } else { + return StridesAccess{strides_.data(), strides_.size()}; + } + } - const Strides& strides() const { return strides_; } + Shape::value_type size(std::ptrdiff_t dim) const { + const auto index = dim < 0 + ? static_cast(shape_.size()) + dim + : dim; + return shape_[static_cast(index)]; + } infini::ops::DataType dtype() const { return dtype_; } @@ -249,6 +304,30 @@ class OwningTensor { return 1; } + OwningTensor silu_input(std::vector(8), {2, 4}); + auto silu_output = infini::ops::SiluAndMul::MakeReturnValue(silu_input); + if (silu_output.shape() != OwningTensor::Shape{2, 2}) { + return 1; + } + + OwningTensor embedding_input(std::vector(6), {2, 3}); + OwningTensor embedding_weight(std::vector(40), {10, 4}); + auto embedding_output = infini::ops::Embedding::MakeReturnValue( + embedding_input, embedding_weight); + if (embedding_output.shape() != OwningTensor::Shape{2, 3, 4}) { + return 1; + } + + OwningTensor query(std::vector(24), {1, 2, 3, 4}); + OwningTensor key(std::vector(40), {1, 2, 5, 4}); + OwningTensor value(std::vector(60), {1, 2, 5, 6}); + auto attention_output = + infini::ops::ScaledDotProductAttention::MakeReturnValue(query, key, + value); + if (attention_output.shape() != OwningTensor::Shape{1, 2, 3, 6}) { + return 1; + } + return 0; } """ diff --git a/tests/test_generate_torch_ops.py b/tests/test_generate_torch_ops.py index 165511180..bd1cfa862 100644 --- a/tests/test_generate_torch_ops.py +++ b/tests/test_generate_torch_ops.py @@ -80,6 +80,14 @@ def test_optional_tensor_params_are_exposed_and_forwarded_to_aten(): assert "std::optional bias" in base assert "bool has_weight_" in base assert "bool has_bias_" in base + assert ( + "weight_shape_{weight ? Tensor::Shape{weight->shape()} : Tensor::Shape{}}" + in base + ) + assert ( + "weight_strides_{weight ? Tensor::Strides{weight->strides()} " + ": Tensor::Strides{}}" in base + ) assert "c10::optional at_weight" in source assert "c10::optional at_bias" in source assert "weight->shape()" in source