libtorch (PyTorch C++)-এর জন্য Bnlang বাইন্ডিং।
এটি একটি বেসরকারি তৃতীয় পক্ষীয় বাইন্ডিং। Meta / PyTorch Foundation-এর সাথে সম্পর্কিত নয়। অফিসিয়াল libtorch C++ prebuilt ব্যবহার করে।
Read this in English — README.en.md
import "torch-bnlang" as tch;
লিখুন(tch.সংস্করণ);
লিখুন(tch.ডিভাইসসমূহ());
ধরি মডেল = tch.জিট_মডিউল.খুলুন("model.pt", { device: "cpu" });
ধরি আউট = মডেল.চালান({
"input_ids": { dtype: "int64", shape: [1, 5], data: [...] },
"attention_mask": { dtype: "int64", shape: [1, 5], data: [...] }
});
// আউট["output"] হলো { dtype, shape, handle } — টেনসর_ডেটা দিয়ে materialize:
ধরি ফলাফল = tch.টেনসর_ডেটা(আউট["output"]["handle"]);
tch.টেনসর_বন্ধ_করুন(আউট["output"]["handle"]);
মডেল.বন্ধ_করুন();
import "torch-bnlang" as tch;
ধরি ওজন = tch.load_safetensors("model.safetensors");
// ওজন = { "model.embed_tokens.weight": { dtype, shape, handle }, ... }
ধরি ids = tch.view(tch.arange(0, 7, 1, "int64"), [1, 7]);
ধরি h = tch.embedding(ওজন["model.embed_tokens.weight"], ids);
// ... এর পরে রয়েছে rms_norm, attention_block, swiglu_mlp ইত্যাদি
transformers-bnlang/lib/architectures/qwen2.bnl এবং llama.bnl-এ পূর্ণ
forward পাসের উদাহরণ আছে।
| নাম | ধরন |
|---|---|
সংস্করণ |
string |
ডিভাইসসমূহ() |
function → list of `"cpu" |
module_load(path, opts) |
function → handle |
module_close(handle) |
function |
module_info(handle) |
function → schema map |
module_run(handle, feeds) |
function → outputs map |
জিট_মডিউল.খুলুন(path, opts) |
factory → module object |
| নাম | ধরন |
|---|---|
tensor_from(spec) |
function → handle map |
টেনসর_ডেটা(handle) |
function → list |
টেনসর_বন্ধ_করুন(handle) |
function |
tensor_argmax_last(handle, vocab) |
function → int |
tensor_sample_last(handle, vocab, temp, top_k, top_p, seed) |
function → int |
| নাম | ধরন |
|---|---|
load_safetensors(path) |
function → { name: handle_map } |
| নাম | কী করে |
|---|---|
embedding(weight, indices) |
টোকেন lookup |
linear(input, weight, bias_or_null) |
matmul + optional bias |
matmul(a, b) |
generic matmul |
rms_norm(input, weight, eps) |
RMS normalization (fp32-internal) |
silu(x) / softmax(x, dim) |
activations |
scaled_dot_product_attention(q, k, v, mask, is_causal) |
torch-এর fused SDPA |
apply_rotary(q, k, cos, sin) |
RoPE (Llama/Qwen split-half) |
cat(tensors, dim) / slice(t, dim, s, e) / transpose(t, d0, d1) / view(t, shape) |
shape ops |
repeat_kv(kv, n_rep) |
GQA expansion |
add(a, b) / mul(a, b) / to_dtype(t, dtype) |
elementwise + dtype |
arange(start, end, step, dtype) / ones(shape, dtype) / zeros(shape, dtype) |
tensor creation |
build_rope_cache(positions, head_dim, theta) |
precompute cos/sin |
attention_block(...) |
fused QKV→reshape→RoPE→KV concat→GQA expand→SDPA→out proj |
swiglu_mlp(x, gate_w, up_w, down_w) |
fused SwiGLU MLP |
| নাম | কী করে |
|---|---|
set_num_threads(n) / get_num_threads() |
intra-op প্যারালেলিজম টিউন |
stdout_write(text) |
newline ছাড়া stdout লিখা (Node-এর process.stdout.write সমতুল্য) |
exit(code) |
তাৎক্ষণিক প্রসেস এক্সিট (libtorch থ্রেডপুল hang এড়ায়) |
যা কাজ করে:
- TorchScript
.ptলোডিং + forward pass .safetensorsসরাসরি লোডিং (কনভার্শন স্টেপ ছাড়া)- Tensor handle table (zero-copy KV-cache reuse downstream LLM loop-এর জন্য)
- Fused
attention_block+swiglu_mlpops - bf16 ↔ fp32 auto cast (লোড টাইমে — CPU-তে দ্রুত)
- Architecture-aware vocab (যেকোনো vocab size কাজ করে)
- InferenceMode globally enabled + CPU thread tuning
পরবর্তী:
- আরো device backend: CUDA, MPS (CMake flag flip + CUDA libtorch দিয়ে)
- আরো architecture forward pass (Mistral, Gemma, Phi-3, GPT-2)
- Image-gen ops: conv2d, group_norm, interpolate (SD / SDXL সমর্থনের জন্য)
- mmap-ভিত্তিক safetensors লোডিং (বড় মডেলের জন্য কম মেমরি)
# Windows
bnl script/install.bnl # deps/windows-x64/ এ libtorch CPU prebuilt নামায় (~250 MB)
.\build.ps1 # cmake configure + build -> build/windows-x64/*.dll# macOS / Linux
bnl script/install.bnl
./build.shCUDA সমর্থনের জন্য:
# CUDA libtorch deps/windows-x64/-এ ম্যানুয়ালি বসান, তারপর:
cmake --preset windows-x64 -DUSE_CUDA=ON
cmake --build build/windows-x64 --config Releaseপ্রতি platform-এ আলাদাভাবে build হয়। bnl runtime import-এর সময় bnl.json-এর
artifact path খোঁজে:
| Triple | বিল্ড artifact |
|---|---|
windows-x64 |
build/windows-x64/torch-bnlang.dll |
linux-x64 |
build/linux-x64/torch-bnlang.so |
darwin-arm64 |
build/darwin-arm64/torch-bnlang.dylib |
darwin-x64 |
build/darwin-x64/torch-bnlang.dylib |
libtorch-এর সঙ্গে আসা runtime DLL/SO গুলো (torch_cpu, c10, fbgemm,
libiomp5md ইত্যাদি) plugin-এর পাশে কপি হয়; bnl core-এর
LOAD_WITH_ALTERED_SEARCH_PATH সেগুলোই আগে খুঁজে।
bnl.json manifest (main + native path)
CMakeLists.txt build config
CMakePresets.json প্রতি platform-এ একটি preset
lib/
index.bnl public API (ইংরেজি + বাংলা re-export)
jit_module.bnl JitModule / জিট_মডিউল
src/ C++ source (publish-এ আসে না)
bnl/plugin.h C ABI
json.hpp vendored nlohmann/json (single-header)
main.cpp bnl_load entry + devices / stdout_write / exit
module.{h,cpp} torch::jit::script::Module handle table + forward routing
tensor.{h,cpp} bnl_value <-> torch::Tensor + handle table + argmax/sample
safetensors.{h,cpp} .safetensors parser
ops.{h,cpp} NN op surface (embedding, linear, rms_norm,
attention_block, swiglu_mlp, ...)
deps/<triple>/ libtorch prebuilt (gitignored; install দ্বারা populate হয়)
build/<triple>/ cmake output (gitignored)
script/
install.bnl libtorch prebuilt fetch + extract
install-metadata.bnl প্রতি platform-এ URL + sha256
MIT. libtorch (BSD-3-Clause), nlohmann/json (MIT) — তৃতীয় পক্ষীয়
attribution NOTICES.md-এ আছে।