Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Tensor::run &Layer::getFunc: Tensor& -> Tensor #121

Merged
merged 1 commit into from
Aug 16, 2024
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
120 changes: 60 additions & 60 deletions src/Layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ class Layer {
bool inited_loaded = false;
static map<string, string> layername_2_tensorname;

Tensor &operator()(Tensor &input) {
Tensor operator()(Tensor input) {
auto ts = run({input}, 1);
return ts[0].get();
return ts[0];
}

Tensor &operator()(Tensor &input0, Tensor &input1) {
Tensor operator()(Tensor input0, Tensor input1) {
auto ts = run({input0, input1}, 1);
return ts[0].get();
return ts[0];
}

private:
Expand Down Expand Up @@ -106,7 +106,7 @@ class Layer {
}
return Module::doLoad;
}
vector<std::reference_wrapper<Tensor>> run(vector<Tensor> inputs, int N=1) {
vector<Tensor> run(vector<Tensor> inputs, int N=1) {
Module::runlistIdx = saved_list_idx;
if (Module::doLoad || !inited_loaded) {
init_run();
Expand Down Expand Up @@ -142,7 +142,7 @@ class Layer {
}
}
if(Module::doLoad){
vector<std::reference_wrapper<Tensor>> output_result = {};
vector<Tensor> output_result = {};
for (const auto &layer_next_name : layer_next_names) {
auto next_name = layername_2_tensorname[layer_next_name];
output_result.push_back(*Tensor::graphs[next_name]);
Expand Down Expand Up @@ -196,7 +196,7 @@ class Layer {
auto end_t = mllm_time_us();
std::cout<<op_->name() << " | "<<Tensor::tensor_status<<" time: " << (end_t - start_t)/1000.0F <<"ms"<< std::endl;
#endif
vector<std::reference_wrapper<Tensor>> output_result = {};
vector<Tensor> output_result = {};
for (const auto &layer_next_name : layer_next_names) {
auto next_name = layername_2_tensorname[layer_next_name];
#ifdef DEBUGSAVETENSOR
Expand All @@ -223,9 +223,9 @@ class Linear final : public Layer {
param_["bias"] = (float)bias;
init(std::move(name), OpType::LINEAR);
}
Tensor &operator()(Tensor &input) {
Tensor operator()(Tensor input) {
auto ts = run({input}, 1);
return ts[0].get();
return ts[0];
}
};

Expand All @@ -236,9 +236,9 @@ class SparseIdLinear final : public Layer {
param_["out_dim_"] = (float)out_dim;
init(std::move(name), OpType::SPARSEIDLINEAR);
}
Tensor &operator()(Tensor &input) {
Tensor operator()(Tensor input) {
auto ts = run({input}, 1);
return ts[0].get();
return ts[0];
}
};

Expand All @@ -249,9 +249,9 @@ class SparseLinear final : public Layer {
param_["out_dim_"] = (float)out_dim;
init(std::move(name), OpType::SPARSELINEAR);
}
Tensor &operator()(Tensor &input) {
Tensor operator()(Tensor input) {
auto ts = run({input}, 1);
return ts[0].get();
return ts[0];
}
};

Expand All @@ -262,9 +262,9 @@ class Predictor final : public Layer {
param_["out_dim"] = (float)out_dim;
init(std::move(name), OpType::PREDICTOR);
}
Tensor &operator()(Tensor &input) {
Tensor operator()(Tensor input) {
auto ts = run({input}, 1);
return ts[0].get();
return ts[0];
}
};

Expand All @@ -277,11 +277,11 @@ class ElasticLinear final : public Layer {
param_["bias"] = (float)bias;
init(std::move(name), OpType::ELASTICLINEAR);
}
Tensor &operator()(Tensor &input0, int activate_input_dim, int activate_output_dim) {
Tensor operator()(Tensor input0, int activate_input_dim, int activate_output_dim) {
auto activate_input_dim_tensor = Tensor(activate_input_dim, backend_);
auto activate_output_dim_tensor = Tensor(activate_output_dim, backend_);
auto ts = run({input0, activate_input_dim_tensor, activate_output_dim_tensor}, 1);
return ts[0].get();
return ts[0];
}
};

Expand All @@ -292,9 +292,9 @@ class SiLU final : public Layer {
SiLU(std::string name) {
init(std::move(name), OpType::SILU);
}
Tensor &operator()(Tensor &input) {
Tensor operator()(Tensor input) {
auto ts = run({input}, 1);
return ts[0].get();
return ts[0];
}
};

Expand All @@ -304,9 +304,9 @@ class ReLU final : public Layer {
ReLU(std::string name) {
init(std::move(name), OpType::RELU);
}
Tensor &operator()(Tensor &input) {
Tensor operator()(Tensor input) {
auto ts = run({input}, 1);
return ts[0].get();
return ts[0];
}
};

Expand All @@ -316,9 +316,9 @@ class ReLUSquaredActivation final : public Layer {
ReLUSquaredActivation(std::string name) {
init(std::move(name), OpType::RELU2);
}
Tensor &operator()(Tensor &input) {
Tensor operator()(Tensor input) {
auto ts = run({input}, 1);
return ts[0].get();
return ts[0];
}
};

Expand All @@ -328,9 +328,9 @@ class GELU final : public Layer {
GELU(std::string name) {
init(std::move(name), OpType::OP_GELU);
}
Tensor &operator()(Tensor &input) {
Tensor operator()(Tensor input) {
auto ts = run({input}, 1);
return ts[0].get();
return ts[0];
}
};

Expand All @@ -340,9 +340,9 @@ class QuickGELU final : public Layer {
explicit QuickGELU(std::string name) {
init(std::move(name), OpType::QUICKGLUE);
}
Tensor &operator()(Tensor &input) {
Tensor operator()(Tensor input) {
auto ts = run({input}, 1);
return ts[0].get();
return ts[0];
}
};

Expand All @@ -367,14 +367,14 @@ class Softmax final : public Layer {
param_["do_causal_mask"] = do_causal_mask;
init(std::move(name), OpType::SOFTMAX);
}
Tensor &operator()(Tensor &input) {
Tensor operator()(Tensor input) {
auto ts = run({input}, 1);
return ts[0].get();
return ts[0];
}
Tensor &operator()(Tensor &input, int axis_classes) {
Tensor operator()(Tensor input, int axis_classes) {
auto axis_classes_tensor = Tensor(axis_classes, backend_);
auto ts = run({input, axis_classes_tensor}, 1);
return ts[0].get();
return ts[0];
}
};

Expand All @@ -385,9 +385,9 @@ class Embedding final : public Layer {
param_["vocab_size"] = vocab_size;
init(std::move(name), OpType::EMBEDDING);
}
Tensor &operator()(Tensor &input) {
Tensor operator()(Tensor input) {
auto ts = run({input}, 1);
return ts[0].get();
return ts[0];
}
};

Expand All @@ -397,14 +397,14 @@ class Causalmask final : public Layer {
explicit Causalmask(std::string name) {
init(std::move(name), OpType::CAUSALMASK);
}
Tensor &operator()(Tensor &input) {
Tensor operator()(Tensor input) {
auto ts = run({input}, 1);
return ts[0].get();
return ts[0];
}
Tensor &operator()(Tensor &input0, int kvcache_seq) {
Tensor operator()(Tensor input0, int kvcache_seq) {
auto kvcache_seq_tensor = Tensor(kvcache_seq, backend_);
auto ts = run({input0, kvcache_seq_tensor}, 1);
return ts[0].get();
return ts[0];
}
};

Expand All @@ -414,9 +414,9 @@ class SlidingWindowMask final : public Layer {
param_["window_size"] = window_size;
init(std::move(name), OpType::SLIDINGWINDOWMASK);
}
Tensor &operator()(Tensor &input) {
Tensor operator()(Tensor input) {
auto ts = run({input}, 1);
return ts[0].get();
return ts[0];
}
};

Expand All @@ -439,9 +439,9 @@ class RoPE final : public Layer {
param_["partial_rotary_factor"] = partial_rotary_factor;
init(std::move(name), OpType::ROPE);
}
Tensor &operator()(Tensor &input) {
Tensor operator()(Tensor input) {
auto ts = run({input}, 1);
return ts[0].get();
return ts[0];
}
};

Expand All @@ -458,9 +458,9 @@ class KVCache final : public Layer {
param_["cache_max"] = cache_max;
init(std::move(name), OpType::KVCACHE);
}
Tensor &operator()(Tensor &input) {
Tensor operator()(Tensor input) {
auto ts = run({input}, 1);
return ts[0].get();
return ts[0];
}
int getCacheSeqLen(){
return op_->getCacheSeqLen();
Expand All @@ -478,9 +478,9 @@ class LayerNorm final : public Layer {
param_["bias"] = (float)bias;
init(std::move(name), OpType::LAYERNORM);
}
Tensor &operator()(Tensor &input) {
Tensor operator()(Tensor input) {
auto ts = run({input}, 1);
return ts[0].get();
return ts[0];
}
};

Expand All @@ -499,9 +499,9 @@ class RMSNorm final : public Layer {
init(std::move(name), OpType::RMSNORM);
}

Tensor &operator()(Tensor &input) {
Tensor operator()(Tensor input) {
auto ts = run({input}, 1);
return ts[0].get();
return ts[0];
}
};

Expand All @@ -512,9 +512,9 @@ class Matmul final : public Layer {
param_["transpose1"] = transpose1;
init(std::move(name), OpType::MATMUL);
}
Tensor &operator()(Tensor &input0, Tensor &input1) {
Tensor operator()(Tensor input0, Tensor input1) {
auto ts = run({input0, input1}, 1);
return ts[0].get();
return ts[0];
}
};

Expand All @@ -539,7 +539,7 @@ class Split final : public Layer {
init(std::move(name), OpType::SPLIT);
}

vector<std::reference_wrapper<Tensor>> operator()(Tensor &input) {
vector<Tensor> operator()(Tensor input) {
return run({input}, (int)param_["split_num"]);
}
};
Expand All @@ -557,9 +557,9 @@ class Convolution2D final : public Layer {
param_["bias"] = (float)bias;
init(std::move(name), OpType::CONVOLUTION2D);
}
Tensor &operator()(Tensor &input) {
Tensor operator()(Tensor input) {
auto ts = run({input}, 1);
return ts[0].get();
return ts[0];
}
};

Expand All @@ -578,9 +578,9 @@ class Convolution3D final : public Layer {
param_["bias"] = (float)bias;
init(std::move(name), OpType::CONVOLUTION3D);
}
Tensor &operator()(Tensor &input) {
Tensor operator()(Tensor input) {
auto ts = run({input}, 1);
return ts[0].get();
return ts[0];
}
};

Expand All @@ -590,9 +590,9 @@ class Concat final : public Layer {
param_["axis"] = (float)axis;
init(std::move(name), OpType::CAT);
}
Tensor &operator()(Tensor &input0, Tensor &input1) {
Tensor operator()(Tensor input0, Tensor input1) {
auto ts = run({input0, input1}, 1);
return ts[0].get();
return ts[0];
}
};

Expand All @@ -606,9 +606,9 @@ class Parameter final : public Layer {
param_["dim"] = dim;
init(std::move(name), OpType::PARAMETER);
}
Tensor &operator()() {
Tensor operator()() {
auto ts = run({}, 1);
return ts[0].get();
return ts[0];
}
};

Expand All @@ -617,9 +617,9 @@ class Position final : public Layer {
explicit Position(std::string name) {
init(std::move(name), OpType::POSITION);
}
Tensor &operator()(Tensor &input) {
Tensor operator()(Tensor input) {
auto ts = run({input}, 1);
return ts[0].get();
return ts[0];
}
};

Expand Down
Loading
Loading