Skip to content

Commit 20713e1

Browse files
Susskind115wooway777
authored andcommitted
Add LayerNorm and vision ops wrappers
1 parent a836991 commit 20713e1

25 files changed

Lines changed: 944 additions & 0 deletions

include/infinicore/nn.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

33
#include "nn/embedding.hpp"
4+
#include "nn/layernorm.hpp"
45
#include "nn/linear.hpp"
56
#include "nn/rmsnorm.hpp"
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#pragma once
2+
3+
#include "../ops.hpp"
4+
#include "module.hpp"
5+
6+
namespace infinicore::nn {
7+
8+
/**
9+
* @brief Layer Normalization
10+
*
11+
* Applies LayerNorm over the last dimension.
12+
*
13+
* Formula: y = (x - mean) / sqrt(var + eps) * weight + bias
14+
*/
15+
class LayerNorm : public Module {
16+
public:
17+
/**
18+
* @brief Construct a LayerNorm layer
19+
*
20+
* @param normalized_shape Size of the feature dimension to normalize (typically hidden_size)
21+
* @param eps Small constant for numerical stability (default: 1e-5)
22+
* @param dtype Data type for the weight/bias (default: DataType::F32)
23+
* @param device Device to create the parameters on
24+
*/
25+
LayerNorm(size_t normalized_shape,
26+
double eps = 1e-5,
27+
const DataType &dtype = DataType::F32,
28+
const Device &device = Device());
29+
30+
/**
31+
* @brief Forward pass: apply LayerNorm
32+
*
33+
* @param x Input tensor of shape (*, normalized_shape)
34+
* @return Normalized tensor with same shape as input
35+
*/
36+
Tensor forward(const Tensor &x) const;
37+
38+
// Module information
39+
size_t normalized_shape() const { return normalized_shape_; }
40+
double eps() const { return eps_; }
41+
DataType dtype() const { return dtype_; }
42+
43+
// String representation
44+
std::string extra_repr() const;
45+
46+
// Accessors for parameters
47+
Tensor weight() const { return weight_; }
48+
Tensor bias() const { return bias_; }
49+
50+
protected:
51+
INFINICORE_NN_PARAMETER(weight);
52+
INFINICORE_NN_PARAMETER(bias);
53+
54+
private:
55+
size_t normalized_shape_;
56+
double eps_;
57+
DataType dtype_;
58+
};
59+
60+
} // namespace infinicore::nn

include/infinicore/ops.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,34 @@
1414
#include "ops/binary_cross_entropy_with_logits.hpp"
1515
#include "ops/causal_softmax.hpp"
1616
#include "ops/cdist.hpp"
17+
#include "ops/conv2d.hpp"
1718
#include "ops/cross_entropy.hpp"
1819
#include "ops/embedding.hpp"
1920
#include "ops/flash_attention.hpp"
2021
#include "ops/fmin.hpp"
2122
#include "ops/fmod.hpp"
23+
#include "ops/gelu.hpp"
24+
#include "ops/gelutanh.hpp"
2225
#include "ops/hardswish.hpp"
2326
#include "ops/hardtanh.hpp"
2427
#include "ops/kv_caching.hpp"
28+
#include "ops/layer_norm.hpp"
29+
#include "ops/linear.hpp"
2530
#include "ops/matmul.hpp"
2631
#include "ops/ones.hpp"
2732
#include "ops/paged_attention.hpp"
2833
#include "ops/paged_attention_prefill.hpp"
2934
#include "ops/paged_caching.hpp"
3035
#include "ops/per_tensor_dequant_i8.hpp"
3136
#include "ops/per_tensor_quant_i8.hpp"
37+
#include "ops/quickgelu.hpp"
3238
#include "ops/random_sample.hpp"
3339
#include "ops/rearrange.hpp"
3440
#include "ops/reciprocal.hpp"
41+
#include "ops/relu.hpp"
3542
#include "ops/rms_norm.hpp"
3643
#include "ops/rope.hpp"
3744
#include "ops/silu.hpp"
3845
#include "ops/silu_and_mul.hpp"
46+
#include "ops/softmax.hpp"
3947
#include "ops/swiglu.hpp"

include/infinicore/ops/conv2d.hpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#pragma once
2+
3+
#include "../device.hpp"
4+
#include "common/op.hpp"
5+
6+
#include <cstddef>
7+
#include <vector>
8+
9+
namespace infinicore::op {
10+
class Conv2d {
11+
public:
12+
using schema = void (*)(Tensor, Tensor, Tensor, Tensor,
13+
const size_t *, const size_t *, const size_t *, size_t);
14+
static void execute(Tensor output,
15+
Tensor input,
16+
Tensor weight,
17+
Tensor bias,
18+
const size_t *pads,
19+
const size_t *strides,
20+
const size_t *dilations,
21+
size_t n);
22+
static common::OpDispatcher<schema> &dispatcher();
23+
};
24+
25+
Tensor conv2d(Tensor input,
26+
Tensor weight,
27+
Tensor bias,
28+
const std::vector<size_t> &pads,
29+
const std::vector<size_t> &strides,
30+
const std::vector<size_t> &dilations);
31+
void conv2d_(Tensor output,
32+
Tensor input,
33+
Tensor weight,
34+
Tensor bias,
35+
const std::vector<size_t> &pads,
36+
const std::vector<size_t> &strides,
37+
const std::vector<size_t> &dilations);
38+
} // namespace infinicore::op

include/infinicore/ops/gelu.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#pragma once
2+
3+
#include "../device.hpp"
4+
#include "common/op.hpp"
5+
6+
namespace infinicore::op {
7+
class Gelu {
8+
public:
9+
using schema = void (*)(Tensor, Tensor);
10+
static void execute(Tensor output, Tensor input);
11+
static common::OpDispatcher<schema> &dispatcher();
12+
};
13+
14+
Tensor gelu(Tensor input);
15+
void gelu_(Tensor output, Tensor input);
16+
} // namespace infinicore::op
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#pragma once
2+
3+
#include "../device.hpp"
4+
#include "common/op.hpp"
5+
6+
namespace infinicore::op {
7+
class GeluTanh {
8+
public:
9+
using schema = void (*)(Tensor, Tensor);
10+
static void execute(Tensor output, Tensor input);
11+
static common::OpDispatcher<schema> &dispatcher();
12+
};
13+
14+
Tensor gelu_tanh(Tensor input);
15+
void gelu_tanh_(Tensor output, Tensor input);
16+
} // namespace infinicore::op
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#pragma once
2+
3+
#include "../device.hpp"
4+
#include "common/op.hpp"
5+
6+
namespace infinicore::op {
7+
class LayerNorm {
8+
public:
9+
using schema = void (*)(Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, float);
10+
static void execute(Tensor output,
11+
Tensor input_standardization,
12+
Tensor input_std_deviation,
13+
Tensor input,
14+
Tensor weight,
15+
Tensor bias,
16+
float epsilon);
17+
static common::OpDispatcher<schema> &dispatcher();
18+
};
19+
20+
Tensor layer_norm(Tensor input, Tensor weight, Tensor bias, float epsilon = 1e-5f);
21+
void layer_norm_(Tensor output,
22+
Tensor input_standardization,
23+
Tensor input_std_deviation,
24+
Tensor input,
25+
Tensor weight,
26+
Tensor bias,
27+
float epsilon = 1e-5f);
28+
} // namespace infinicore::op
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#pragma once
2+
3+
#include "../device.hpp"
4+
#include "common/op.hpp"
5+
6+
namespace infinicore::op {
7+
class QuickGelu {
8+
public:
9+
using schema = void (*)(Tensor, Tensor);
10+
static void execute(Tensor output, Tensor input);
11+
static common::OpDispatcher<schema> &dispatcher();
12+
};
13+
14+
Tensor quick_gelu(Tensor input);
15+
void quick_gelu_(Tensor output, Tensor input);
16+
} // namespace infinicore::op

include/infinicore/ops/relu.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#pragma once
2+
3+
#include "../device.hpp"
4+
#include "common/op.hpp"
5+
6+
namespace infinicore::op {
7+
class Relu {
8+
public:
9+
using schema = void (*)(Tensor, Tensor);
10+
static void execute(Tensor output, Tensor input);
11+
static common::OpDispatcher<schema> &dispatcher();
12+
};
13+
14+
Tensor relu(Tensor input);
15+
void relu_(Tensor output, Tensor input);
16+
} // namespace infinicore::op

include/infinicore/ops/softmax.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#pragma once
2+
3+
#include "../device.hpp"
4+
#include "common/op.hpp"
5+
6+
namespace infinicore::op {
7+
class Softmax {
8+
public:
9+
using schema = void (*)(Tensor, Tensor, int);
10+
static void execute(Tensor output, Tensor input, int axis);
11+
static common::OpDispatcher<schema> &dispatcher();
12+
};
13+
14+
Tensor softmax(Tensor input, int axis = -1);
15+
void softmax_(Tensor output, Tensor input, int axis = -1);
16+
} // namespace infinicore::op

0 commit comments

Comments
 (0)