Skip to content
Open
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
20 changes: 19 additions & 1 deletion core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ set(sources_headers
SOFIE/RModel.hxx
SOFIE/RModelProfiler.hxx
SOFIE/RModelProfilerGPU.hxx
SOFIE/RWeightFile.hxx
SOFIE/ROperator.hxx
SOFIE/ROperator_BasicUnary.hxx
SOFIE/ROperator_BasicBinary.hxx
Expand All @@ -27,6 +28,16 @@ set(sources_headers
SOFIE/ROperator_Conv.hxx
SOFIE/ROperator_ConvTranspose.hxx
SOFIE/ROperator_Gemm.hxx
SOFIE/ROperator_QuantizedGemm.hxx
SOFIE/ROperator_QuantizedMatMul.hxx
SOFIE/ROperator_QuantizedMatrix.hxx
SOFIE/SOFIE_Quantized.hxx
SOFIE/SOFIE_QuantizedAlpaka.hxx
SOFIE/RQuantization.hxx
SOFIE/RQuantization_Analysis.hxx
SOFIE/RQuantization_DenseLinear.hxx
SOFIE/RQuantization_Parameters.hxx
SOFIE/RQuantization_Storage.hxx
SOFIE/ROperator_Relu.hxx
SOFIE/ROperator_Tanh.hxx
SOFIE/ROperator_LeakyRelu.hxx
Expand Down Expand Up @@ -57,6 +68,8 @@ set(sources_headers
SOFIE/ROperator_Split.hxx
SOFIE/ROperator_SubGraph.hxx
SOFIE/ROperator_Pad.hxx
SOFIE/ROperator_QONNXQuant.hxx
SOFIE/ROperator_ONNXQuantizeLinear.hxx
SOFIE/ROperator_Where.hxx
SOFIE/ROperator_Einsum.hxx
SOFIE/ROperator_Random.hxx
Expand All @@ -78,6 +91,12 @@ list(TRANSFORM sources_headers PREPEND "inc/")
set(sources_cxx
src/RModel_Base.cxx
src/RModel.cxx
src/RModel_Quantization.cxx
src/RWeightFile.cxx
src/RQuantization_Analysis.cxx
src/RQuantization_DenseLinear.cxx
src/RQuantization_Parameters.cxx
src/RQuantization_Storage.cxx
src/RModelProfiler.cxx
src/RModelProfilerGPU.cxx
src/RModel_ALPAKA.cxx
Expand Down Expand Up @@ -120,4 +139,3 @@ install(TARGETS SOFIE_core
install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/inc/"
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)

3 changes: 3 additions & 0 deletions core/inc/SOFIE/OperatorList.hxx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include "SOFIE/ROperator_Transpose.hxx"
#include "SOFIE/ROperator_Gemm.hxx"
#include "SOFIE/ROperator_QuantizedGemm.hxx"
#include "SOFIE/ROperator_QuantizedMatMul.hxx"
#include "SOFIE/ROperator_ONNXQuantizeLinear.hxx"
#include "SOFIE/ROperator_Relu.hxx"
#include "SOFIE/ROperator_Tanh.hxx"
#include "SOFIE/ROperator_LeakyRelu.hxx"
Expand Down
34 changes: 34 additions & 0 deletions core/inc/SOFIE/RModel.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "SOFIE/RModel_Base.hxx"
#include "SOFIE/SOFIE_common.hxx"
#include "SOFIE/RQuantization.hxx"
#include "SOFIE/ROperator.hxx"


Expand Down Expand Up @@ -32,6 +33,7 @@ private:
std::unordered_map<std::string, InitializedTensor> fInitializedTensors;
std::unordered_map<std::string, TensorInfo> fIntermediateTensorInfos;
std::unordered_map<std::string, DynamicTensorInfo> fDynamicTensorInfos;
QuantizationModelState fQuantizationState; // quantization metadata, regions, storage, and backend lowering plans
std::unordered_map<std::string, std::pair<std::vector<Dim>, bool>> fShapeTensors; // constant tensors describing a shape
std::unordered_map<std::string, std::string> fAliasTensors; // alias tensors (name -> original tensor name)
std::unordered_map<std::string, std::string>
Expand All @@ -42,6 +44,11 @@ private:

std::vector<std::unique_ptr<ROperator>> fOperators;

// transient lowered operator view while the parsed fOperators graph remains intact.
// let code generation use synthetic operators for proven regions.
std::unordered_map<std::size_t, std::unique_ptr<ROperator>> fLoweredOperators;
std::set<std::size_t> fLoweredConsumedOperatorIndices;

std::vector<std::shared_ptr<RModel>> fSubGraphs; ///<! sub-graph models (transient)
RModel * fParentGraph = nullptr;

Expand Down Expand Up @@ -74,6 +81,10 @@ private:
/// GPU-only pass: fuse GEMM→LeakyReLU (and GEMM→ReLU where not already
/// handled by the ONNX parser) into a single in-place kernel sequence.
void FuseGemmActivations_GPU();
void BuildLoweredOperatorView(EQuantizedBackend backend = EQuantizedBackend::CPU);
void PrepareQuantizedTensorStorage(EQuantizedBackend backend);
void AddQuantizedGeneratedHeaders(EQuantizedBackend backend = EQuantizedBackend::CPU);
void AddLoweredQuantizedOperators(EQuantizedBackend backend = EQuantizedBackend::CPU);

public:
// Rule of five: explicitly define move semantics, disallow copy
Expand All @@ -100,6 +111,17 @@ public:
ETensorType GetTensorType(std::string name) const;
std::vector<Dim> GetDynamicTensorShape(const std::string & name) const ;

void AddQuantizationInfo(const std::string & tensor_name, QuantizationInfo info);
bool HasQuantizationInfo(const std::string & tensor_name) const;
const QuantizationInfo & GetQuantizationInfo(const std::string & tensor_name) const;

void RegisterQuantizedTensorStorage(QuantizedTensorStorage storage);
bool HasQuantizedTensorStorage(const std::string & storage_tensor_name) const;
const QuantizedTensorStorage & GetQuantizedTensorStorage(const std::string & storage_tensor_name) const;

void AnalyzeQuantizedRegions();
const QuantizationModelState & GetQuantizationState() const { return fQuantizationState; }

// get the values for the tensor representing a shape
const std::vector<Dim> & GetShapeTensorValues(const std::string & tensor_name) const;

Expand Down Expand Up @@ -145,6 +167,18 @@ public:
AddInitializedTensor(tensor_name, GetTemplatedType(T()), shape, data);
}

template <typename T>
void AddInitializedTensor(const std::string & tensor_name, const std::vector<std::size_t> & shape, const std::vector<T> &values)
{
size_t size = ConvertShapeToLength(shape);
if (values.size() != size) {
throw std::runtime_error("sofie: initialized tensor " + tensor_name + " data length does not match shape");
}
std::shared_ptr<void> data(malloc(size * sizeof(T)), free);
std::copy(values.begin(), values.end(), static_cast<T *>(data.get()));
AddInitializedTensor(tensor_name, GetTemplatedType(T()), shape, data);
}

void AddShapeTensor(const std::string & name, const std::vector<Dim> & shapeValues, bool scalar = false);
void AddAliasTensor(const std::string & name, const std::string & origin);
bool IsAliasTensor(const std::string & tensor_name) const;
Expand Down
4 changes: 3 additions & 1 deletion core/inc/SOFIE/RModel_Base.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ enum class Options {
kGNN = 0x8,
kGNNComponent = 0x10,
kProfile = 0x20,
kBinaryWeightFile = 0x40,
kTextWeightFile = 0x80,
};

// Optimization levels inspired by ONNXRuntime.
Expand All @@ -39,7 +41,7 @@ enum class OptimizationLevel {
kExtended = 0x1,
};

enum class WeightFileType { None, RootBinary, Text };
enum class WeightFileType { None, RootBinary, Text, Binary };


inline std::underlying_type_t<Options> operator|(Options opA, Options opB) {
Expand Down
15 changes: 14 additions & 1 deletion core/inc/SOFIE/ROperator.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,16 @@ enum class OperatorKind {
UNARY_COS=22,
UNARY_ABS=23,
CLIP=24,
NOT=25
NOT=25,
QUANTIZED_GEMM=26,
QUANTIZED_MATMUL=27
};

inline const char* toString(OperatorKind kind) {
switch (kind) {
case OperatorKind::GEMM: return "GEMM";
case OperatorKind::QUANTIZED_GEMM: return "QUANTIZED_GEMM";
case OperatorKind::QUANTIZED_MATMUL: return "QUANTIZED_MATMUL";
case OperatorKind::LAYERNORM: return "LAYERNORM";
case OperatorKind::RELU: return "RELU";
case OperatorKind::CONSTANT: return "CONSTANT";
Expand Down Expand Up @@ -82,6 +86,15 @@ public:
virtual std::string GetBlasConfig() { return ""; }
virtual void UpdateFusableTensorName(std::string, const std::function<void(const std::string&)>& removal_func){ return;};

// Semantic graph-analysis hooks
virtual bool IsQuantizationBoundary() const { return false; }
virtual std::string GetQuantizationSourceTensor() const
{
if (fInputTensorNames.empty())
return {};
return std::string(fInputTensorNames.front());
}

// Elementwise kernel fusion interface
virtual bool IsElementwise() const { return false; }
// Returns the C++ expression applying this op to inputVar (a local T variable) for fused kernel generation
Expand Down
15 changes: 15 additions & 0 deletions core/inc/SOFIE/ROperator_Gemm.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <iomanip>
#include <limits>
#include <cassert>
#include <utility>


namespace SOFIE{
Expand Down Expand Up @@ -72,6 +73,20 @@ namespace SOFIE{
fOutputTensorNames = { fNY };
fKind = OperatorKind::GEMM;
}
// Getters for further quantized GEMM lowering-elimination.
float GetAlpha() const { return fAttrAlpha; }
float GetBeta() const { return fAttrBeta; }
int_t GetTransA() const { return fAttrTransA; }
int_t GetTransB() const { return fAttrTransB; }
bool HasBias() const { return !fNC.empty(); }
const std::string &GetInputTensorName() const { return fNA; }
const std::string &GetWeightTensorName() const { return fNB; }
const std::string &GetBiasTensorName() const { return fNC; }
const std::string &GetOutputTensorName() const { return fNY; }
const std::vector<Dim> &GetInputShape() const { return fShapeA; }
const std::vector<Dim> &GetWeightShape() const { return fShapeB; }
const std::vector<Dim> &GetOutputShape() const { return fShapeY; }
std::vector<std::string> GetStdLibs() override { return { std::string("cmath"), std::string("cstdint"), std::string("vector") }; }

std::vector<ETensorType> TypeInference(std::vector<ETensorType> input) override {
ETensorType out = input[0];
Expand Down
Loading