gcw_6a3w2yXz/tiny-random-LlamaForCausalLM-NPU-model
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

hmellor/tiny-random-LlamaForCausalLM - Ascend NPU Adapted

This is an Ascend NPU-adapted deployment for hmellor/tiny-random-LlamaForCausalLM (LlamaForCausalLM, text-generation).

  • Original model: https://huggingface.co/hmellor/tiny-random-LlamaForCausalLM
  • Architecture: LlamaForCausalLM (2 layers, hidden_size 16, 4 heads)
  • Route: text-generation (AutoModelForCausalLM)
  • Device: npu:0 with synchronized timing and CPU consistency validation
  • Task: Causal LM generation with greedy decoding (do_sample=False, max_new_tokens=32)

NPU Device Proof

Model and tensors are explicitly placed on npu:0 and verified:

  • next(model.parameters()).device == npu:0
  • input_ids.device == npu:0
  • output token ids device == npu:0
  • logits device == npu:0

Synchronization uses torch.npu.synchronize() before/after timing.

Consistency Validation

  • CPU vs NPU token ids identical on prompts:
    • "Hello, my name is"
    • "The future of artificial intelligence is"
  • Logits max diff ~0.35 (fp32) but generation greedy ids fully match
  • Real prompts, no dummy inputs

Performance (Ascend 910B)

  • First compile+inference ~0.52s, stable avg ~0.18s for 2 prompts, max_new_tokens=32
  • ~212 tokens/s (after compilation)

Validated on Ascend 910 (2x NPU, 61 GiB free per card, torch_npu 2.9.0.post1).

Usage

from transformers import AutoTokenizer, AutoModelForCausalLM
import torch, torch_npu

model_dir = "./model"  # or local path
tokenizer = AutoTokenizer.from_pretrained(model_dir)
model = AutoModelForCausalLM.from_pretrained(model_dir, torch_dtype=torch.float32).to("npu:0")
inputs = tokenizer(["Hello, my name is"], return_tensors="pt").to("npu:0")
outputs = model.generate(**inputs, max_new_tokens=32, do_sample=False)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))

See inference.py for full synchronized example.

Evidence

Agent Workflow NPU Device Call Model Result

Files

  • inference.py - NPU inference with device proof and perf
  • requirements.txt - dependencies
  • assets/*.png - xterm.js evidence screenshots