Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions scripts/generate_torch_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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} ? "
Expand Down
3 changes: 1 addition & 2 deletions src/base/add.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <vector>

#include "operator.h"

Expand Down Expand Up @@ -68,7 +67,7 @@ class Add : public Operator<Add> {
auto input_shape = input.shape();
auto other_shape = other.shape();
auto ndim = std::max(input_shape.size(), other_shape.size());
std::vector<std::size_t> 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()
Expand Down
11 changes: 7 additions & 4 deletions src/base/batch_norm_elemt.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@ class BatchNormElemt : public Operator<BatchNormElemt> {
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()} {}
Expand Down
6 changes: 4 additions & 2 deletions src/base/binary_cross_entropy.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ class BinaryCrossEntropy : public Operator<BinaryCrossEntropy> {
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()} {}
Expand Down
6 changes: 4 additions & 2 deletions src/base/binary_cross_entropy_backward.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ class BinaryCrossEntropyBackward : public Operator<BinaryCrossEntropyBackward> {
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()} {}
Expand Down
8 changes: 4 additions & 4 deletions src/base/clamp.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ class Clamp : public Operator<Clamp> {
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()} {}

Expand Down
8 changes: 4 additions & 4 deletions src/base/clip.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ class Clip : public Operator<Clip> {
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()} {}

Expand Down
6 changes: 4 additions & 2 deletions src/base/conv_infinilm.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ class ConvInfinilm : public Operator<ConvInfinilm> {
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()},
Expand Down
12 changes: 8 additions & 4 deletions src/base/diff.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@ class Diff : public Operator<Diff> {
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},
Expand Down
2 changes: 1 addition & 1 deletion src/base/embedding.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class Embedding : public Operator<Embedding> {
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());
Expand Down
73 changes: 43 additions & 30 deletions src/base/flash_attn_with_kvcache.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,46 +90,59 @@ class FlashAttnWithKvcache : public Operator<FlashAttnWithKvcache> {
: 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()},
Expand Down
12 changes: 8 additions & 4 deletions src/base/histogram.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ class Histogram : public Operator<Histogram> {
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()} {}
Expand All @@ -46,8 +48,10 @@ class Histogram : public Operator<Histogram> {
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},
Expand Down
11 changes: 7 additions & 4 deletions src/base/internal_batch_norm_with_update.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,15 @@ class BatchNormWithUpdate : public Operator<BatchNormWithUpdate> {
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},
Expand Down
5 changes: 3 additions & 2 deletions src/base/internal_conv_depthwise2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ class ConvDepthwise2d : public Operator<ConvDepthwise2d> {
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},
Expand Down
22 changes: 14 additions & 8 deletions src/base/internal_native_batch_norm_legit.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,15 @@ class NativeBatchNormLegit : public Operator<NativeBatchNormLegit> {
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},
Expand All @@ -62,12 +65,15 @@ class NativeBatchNormLegit : public Operator<NativeBatchNormLegit> {
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},
Expand Down
5 changes: 3 additions & 2 deletions src/base/internal_slow_conv2d_forward.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ class SlowConv2dForward : public Operator<SlowConv2dForward> {
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},
Expand Down
Loading
Loading