Model Details
Model Description
This model was developed as part of the CE893-7-AU-CO (Generative Artificial Intelligence) module at the University of Essex. It adapts a lightweight 1.5B-parameter instruction-tuned language model to emit syntactically valid Terraform HCL without markdown fences, extraneous prose, or unrequested resources.
- Developed by: Ali Ahmed Hamad
- Model type: Causal Language Model (16-bit bf16 LoRA fine-tuned, merged; also exported as F16 GGUF)
- Language(s): English (input), HashiCorp Configuration Language / Terraform HCL (output)
- License: Apache 2.0 (inherited from
Qwen/Qwen2.5-1.5B-Instruct)
- Finetuned from model:
Qwen/Qwen2.5-1.5B-Instruct (revision 989aa7980e4cf806f80c7fef2b1adb7bc71aa306)
Uses
Direct Use
Intended for cloud engineers, DevOps practitioners, and developers who want to generate starter Terraform HCL configurations from plain-English infrastructure descriptions across multi-cloud providers.
Out-of-Scope Use
- Unreviewed production deployments: Generated configurations must always be validated (
terraform validate, terraform plan) and security-audited before running terraform apply.
- General-purpose chat or non-IaC coding: Fine-tuning is specialized for Terraform HCL; general conversational performance may be reduced compared to the base model.
Bias, Risks, and Limitations
- Provider schema drift: Terraform provider arguments evolve frequently; the model may occasionally output deprecated attributes or omit newly required fields.
- Security defaults: Generated HCL may use permissive defaults (e.g., open CIDR blocks, unencrypted storage) unless explicitly constrained in the prompt.
- Unsupported additions: Even after fine-tuning and prompt constraints, 28.6% of held-out test outputs included extra resource blocks not present in the reference configuration.
Recommendations
Always run terraform fmt, terraform validate, and terraform plan (alongside a static security scanner such as tfsec or checkov) on any generated output before provisioning live cloud resources.
How to Get Started with the Model
import torchfrom transformers import AutoModelForCausalLM, AutoTokenizer model_id = "alihamad24/qwen2.5-1.5b-terraform-merged" tokenizer = AutoTokenizer.from_pretrained(model_id)model = AutoModelForCausalLM.from_pretrained( model_id, torch_dtype=torch.bfloat16, device_map="auto",) messages = [ { "role": "system", "content": ( "You are an expert cloud infrastructure engineer. " "Translate the user's natural language infrastructure request into valid, deployable Terraform HCL code. " "Do not include any extra resources, variables, or outputs that are not explicitly requested. " "Emit ONLY valid Terraform HCL with no markdown fences and no extra explanation." ), }, { "role": "user", "content": "Write a Terraform configuration for AWS to create an S3 bucket named 'my-app-data' with a private ACL.", },] text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)inputs = tokenizer([text], return_tensors="pt").to(model.device) with torch.no_grad(): outputs = model.generate(**inputs, max_new_tokens=2048, do_sample=False) generated = outputs[:, inputs["input_ids"].shape[1]:]print(tokenizer.decode(generated[0], skip_special_tokens=True))
Training Details
Training Data
Fine-tuned on a cleaned, verified subset of SASVAAI/terraform-multicloud (15,000 raw records inspected).
Preprocessing
- Syntax verification: Every reference HCL block was stripped of markdown fences and verified with both
python-hcl2 and the official HashiCorp terraform fmt - CLI; invalid records were discarded.
- Leakage-free splitting: Exact duplicates were removed, and near-duplicate prompts (TF-IDF cosine similarity ≥ 0.90) were grouped into connected components before splitting into 10,000 train / 1,000 validation / 1,000 held-out test examples so paraphrases never leak across splits.
Training Procedure
Training Hyperparameters
- Training regime: bf16 mixed precision (standard 16-bit LoRA,
use_qlora=False)
- Loss masking: Assistant-only loss (
assistant_only_loss=True)
- LoRA config:
r=32, lora_alpha=64, lora_dropout=0.1, bias="none", target modules: q_proj, k_proj, v_proj, o_proj, gate_proj, up_proj, down_proj
- Optimizer & schedule: Cosine learning rate schedule,
learning_rate=1.5e-4, weight_decay=0.01, warmup_ratio=0.05
- Batch size: 4 per device × 4 gradient accumulation steps (effective batch size 16)
- Epochs: 3 (1,875 optimizer steps; ~4.70 hours total runtime)
- Max sequence length: 2048
- Seed: 42
- Checkpoint selection: Best validation checkpoint selected by lowest
eval_loss (0.4231 at step 1,200 / epoch 1.92)
Evaluation
Testing Data & Metrics
Evaluated on 1,000 held-out test examples from SASVAAI/terraform-multicloud under frozen greedy decoding (do_sample=False, max_new_tokens=2048). Each output is scored using a 3-point scheme:
- Score 2 (Fully Correct): Passes
python-hcl2 and terraform fmt -, and matches all expected Terraform resource types with no unsupported additions.
- Score 1 (Partly Correct): Valid HCL syntax, but misses required resource types or introduces unsupported resource blocks.
- Score 0 (Incorrect): Fails HCL parsing or
terraform fmt -.
Results
Table with columns: Condition, Valid HCL Syntax (%), Score 2 — Fully Correct (%), Score 1 — Partly Correct (%), Score 0 — Invalid (%), Unsupported Additions (%), Mean Score (0–2)| Condition | Valid HCL Syntax (%) | Score 2 — Fully Correct (%) | Score 1 — Partly Correct (%) | Score 0 — Invalid (%) | Unsupported Additions (%) | Mean Score (0–2) |
|---|
| Zero-Shot Baseline | 76.1% | 21.8% | 54.3% | 23.9% | 41.5% | 0.979 |
| Few-Shot Baseline (3-shot) | 69.6% | 21.3% | 48.3% | 30.4% | 31.0% | 0.909 |
| LoRA Fine-Tuned (This Model) | 89.7% | 48.0% | 41.7% | 10.3% | 28.6% | 1.377 |
Summary
On the expanded 1,000-example held-out test set, LoRA fine-tuning increased valid HCL syntax generation from 76.1% (zero-shot) and 69.6% (few-shot) to 89.7%, more than doubled the rate of fully correct configurations (48.0% vs. 21.8% zero-shot / 21.3% few-shot), reduced invalid outputs from 23.9% down to 10.3%, and lowered unsupported additions to 28.6%.
Technical Specifications
Compute Infrastructure
- Hardware: NVIDIA L4 (22.03 GB VRAM) on Google Colab Pro
- Software: Python 3.13.16, PyTorch 2.14.1+cu130,
transformers 5.24.1, peft 0.22.0, trl 1.19.0, datasets 5.2.0, tokenizers 0.23.3, python-hcl2, Terraform CLI v1.9.8
- Author: Ali Ahmed Hamad (University of Essex)