-
Notifications
You must be signed in to change notification settings - Fork 58
issue/194: Support Quantization Config and Quanted Model Inference #195
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
Open
qinyiqun
wants to merge
8
commits into
InfiniTensor:main
Choose a base branch
from
qinyiqun:dev
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
01a7396
支持nv w8 1batch 1tp
qinyiqun 9e27583
增加json支持
qinyiqun bc07f04
InfiniLM 增加量化层和global config
qinyiqun 1d8f51b
以一种比较优雅的方式增加了quant config的支持
qinyiqun 6898b48
修改部分代码结构,删除无用代码
qinyiqun 23c2ae3
跟随inifnicore修改
qinyiqun ca87660
删除所有的model_config,统一使用global_config
qinyiqun 6615743
跟随InfiniLM最新代码修改
qinyiqun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,6 @@ | ||
| [submodule "third_party/spdlog"] | ||
| path = third_party/spdlog | ||
| url = https://github.com/gabime/spdlog.git | ||
| [submodule "third_party/json"] | ||
| path = third_party/json | ||
| url = https://github.com/nlohmann/json.git |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| #include "global_config.hpp" | ||
|
|
||
| namespace infinilm::config::global_config { | ||
| GlobalConfig::GlobalConfig(const std::string &path) { | ||
| std::ifstream file(path); | ||
| if (file.is_open()) { | ||
| file >> config_json; | ||
| file.close(); | ||
| } else { | ||
| throw std::runtime_error("Could not open config file: " + path); | ||
| } | ||
| this->quant_config = quantization::QuantConfig(config_json["quantization_config"]); | ||
| } | ||
|
|
||
| infinicore::nn::QuantScheme | ||
| GlobalConfig::get_quant_scheme() const { | ||
| if (quant_config.get_quant_scheme() != infinicore::nn::QuantScheme::NONE) { | ||
| return quant_config.get_quant_scheme(); | ||
| } else { | ||
| return infinicore::nn::QuantScheme::NONE; | ||
| } | ||
| } | ||
|
|
||
| std::shared_ptr<infinicore::nn::RoPE::ScalingConfig> | ||
| GlobalConfig::get_rope_scaling() const { | ||
| if (!config_json.contains("rope_scaling") || config_json["rope_scaling"].is_null()) { | ||
| return nullptr; | ||
| } | ||
|
|
||
| const auto &rope_scaling = config_json["rope_scaling"]; | ||
| if (!rope_scaling.is_object()) { | ||
| throw std::runtime_error("rope_scaling must be an object"); | ||
| } | ||
|
|
||
| if (!rope_scaling.contains("type")) { | ||
| throw std::runtime_error("rope_scaling must contain 'type' field"); | ||
| } | ||
|
|
||
| std::string type_str = rope_scaling["type"].get<std::string>(); | ||
| if (type_str == "longrope") { | ||
| // Required fields for LongRopeConfig | ||
| if (!rope_scaling.contains("short_factor") || !rope_scaling.contains("long_factor") || !rope_scaling.contains("original_max_position_embeddings")) { | ||
| throw std::runtime_error( | ||
| "LongRopeConfig requires 'short_factor', 'long_factor', and 'original_max_position_embeddings'"); | ||
| } | ||
|
|
||
| auto short_factor = rope_scaling["short_factor"].get<std::vector<float>>(); | ||
| auto long_factor = rope_scaling["long_factor"].get<std::vector<float>>(); | ||
| size_t original_max_position_embeddings = rope_scaling["original_max_position_embeddings"].get<size_t>(); | ||
|
|
||
| float factor = 1.0f; | ||
| if (rope_scaling.contains("factor")) { | ||
| factor = rope_scaling["factor"].get<float>(); | ||
| } | ||
|
|
||
| return std::make_shared<infinicore::nn::RoPE::LongRopeConfig>( | ||
| std::move(short_factor), | ||
| std::move(long_factor), | ||
| original_max_position_embeddings, | ||
| factor); | ||
| } else if (type_str == "default" || type_str == "none") { | ||
| // Default scaling, no scaling applied | ||
| return nullptr; | ||
| } else { | ||
| throw std::runtime_error("Unsupported rope_scaling type: " + type_str); | ||
| } | ||
| } | ||
|
|
||
| infinicore::DataType | ||
| GlobalConfig::get_dtype() const { | ||
| try { | ||
| std::string dtype_str = this->get<std::string>("torch_dtype"); | ||
| if (dtype_str == "float32") { | ||
| return infinicore::DataType::F32; | ||
| } else if (dtype_str == "float16") { | ||
| return infinicore::DataType::F16; | ||
| } else if (dtype_str == "bfloat16") { | ||
| return infinicore::DataType::BF16; | ||
| } else if (dtype_str == "int8") { | ||
| return infinicore::DataType::I8; | ||
| } else { | ||
| throw std::runtime_error("Unsupported dtype string: " + dtype_str); | ||
| } | ||
| } catch (const std::exception &e) { | ||
| throw std::runtime_error("Error getting dtype from config: " + std::string(e.what())); | ||
| } | ||
| } | ||
| } // namespace infinilm::config::global_config |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| #pragma once | ||
|
|
||
| // #include "infinicore/nn/quantization.hpp" | ||
| #include "infinicore/nn/rope.hpp" | ||
| #include "infinicore/ops.hpp" | ||
| #include "quant_config.hpp" | ||
| #include <fstream> | ||
| #include <string> | ||
|
|
||
| namespace infinilm::config::global_config { | ||
| struct GlobalConfig { | ||
| // Global config is implemented using nlohmann/json and is primarily used for advanced configuration | ||
| // beyond the standard model config. It is initialized via GlobalConfig(const std::string& path) | ||
| // and passed through the InferEngine during inference. | ||
| public: | ||
| GlobalConfig() = default; | ||
| GlobalConfig(const nlohmann::json &json) : config_json(json) {}; | ||
| GlobalConfig(const std::string &path); | ||
|
|
||
| // Template Function to get a value by key with type safety | ||
| template <typename T> | ||
| T get(const std::string &key) const { | ||
| if (!config_json.contains(key)) { | ||
| throw std::out_of_range("Key '" + key + "' not found in config."); | ||
| } | ||
| try { | ||
| return config_json.at(key).get<T>(); | ||
| } catch (const nlohmann::json::type_error &e) { | ||
| throw std::runtime_error("Type conversion failed for key '" + key + "': " + std::string(e.what())); | ||
| } | ||
| } | ||
|
|
||
| template <typename T> | ||
| T get_or(const std::string &key, const T &default_value) const { | ||
| if (!config_json.contains(key) || config_json.at(key).is_null()) { | ||
| return default_value; | ||
| } | ||
| try { | ||
| return config_json.at(key).get<T>(); | ||
| } catch (const nlohmann::json::type_error &) { | ||
| // If type conversion fails, return default value | ||
| return default_value; | ||
| } | ||
| } | ||
| size_t get_kv_dim() const { | ||
| return get<size_t>("hidden_size") * get<size_t>("num_key_value_heads") / get<size_t>("num_attention_heads"); | ||
| } | ||
| size_t get_head_dim() const { | ||
| if (config_json.contains("head_dim")) { | ||
| return get<size_t>("head_dim"); | ||
| } | ||
| return get<size_t>("hidden_size") / get<size_t>("num_attention_heads"); | ||
| } | ||
|
|
||
| infinicore::DataType get_dtype() const; | ||
| infinicore::nn::QuantScheme get_quant_scheme() const; | ||
| std::shared_ptr<infinicore::nn::RoPE::ScalingConfig> get_rope_scaling() const; | ||
|
|
||
| private: | ||
| nlohmann::json config_json; | ||
| quantization::QuantConfig quant_config; | ||
| }; | ||
| } // namespace infinilm::config::global_config | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| #include "quant_config.hpp" | ||
|
|
||
| namespace infinilm::config::quantization { | ||
| QuantConfig::QuantConfig(const nlohmann::json &json) : quantization_config(json) { | ||
| this->quantization_method = get_quantization_method(); | ||
| } | ||
|
|
||
| std::shared_ptr<infinilm::quantization::BaseQuantization> | ||
| QuantConfig::get_quantization_method() const { | ||
| if (quantization_config.is_null()) { | ||
| return nullptr; | ||
| } | ||
|
|
||
| // Determine the quantization scheme from the JSON config | ||
| if (quantization_config["quant_method"] == "compressed-tensors") { | ||
| return std::make_shared<infinilm::quantization::CompressedTensors>(quantization_config); | ||
| } | ||
| // Add other schemes as needed | ||
|
|
||
| return nullptr; // Default case if no matching scheme | ||
| } | ||
| } // namespace infinilm::config::quantization |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| #pragma once | ||
| #include "../quantization/quantization.hpp" | ||
| #include "nlohmann/json.hpp" | ||
|
|
||
| namespace infinilm::config::quantization { | ||
|
|
||
| class QuantConfig { | ||
| // QuantConfig is used to store and parse the "quantization" field from config.json. | ||
| // This is currently a basic version and will be extended in the future. | ||
| public: | ||
| QuantConfig() = default; | ||
| QuantConfig(const nlohmann::json &json); | ||
|
|
||
| infinicore::nn::QuantScheme get_quant_scheme() const { | ||
| if (quantization_method != nullptr) { | ||
| return quantization_method->get_quant_scheme(); | ||
| } else { | ||
| return infinicore::nn::QuantScheme::NONE; | ||
| } | ||
| } | ||
|
|
||
| private: | ||
| nlohmann::json quantization_config; | ||
| std::shared_ptr<infinilm::quantization::BaseQuantization> get_quantization_method() const; | ||
| std::shared_ptr<infinilm::quantization::BaseQuantization> quantization_method; | ||
| }; | ||
|
|
||
| } // namespace infinilm::config::quantization |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
外部无法看到config_json,也就是无法从config json里拿到其它的field?这个class只为了哪一个quant scheme?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个留下来是为了扩展性,未来加入新的feature可能会需要。
以及这个类以后大概是会放更多的功能进去,quant_scheme只是现在的quant_config给出的