Salesforce CodeGen Tutorial: Generate, Validate, and Rerank Python Functions With Unit Tests and Safety Checks Salesforce released a tutorial demonstrating an end-to-end workflow for its CodeGen model, covering loading from Hugging Face, generating Python functions from natural-language prompts, and adding validation steps including syntax checking, static safety checks, unit-test-based validation, best-of-N candidate reranking, and artifact export. The tutorial shows how CodeGen can be used as part of a structured code-generation pipeline that evaluates and filters generated solutions. In this tutorial, we implement an end-to-end workflow for Salesforce CodeGen https://github.com/salesforce/CodeGen . We load a CodeGen model from Hugging Face, prepare it for code generation, and use it to generate Python functions from natural-language prompts. We then move beyond basic inference by adding function extraction, syntax checking, static safety checks, unit-test-based validation, best-of-N candidate reranking, multi-step program synthesis, prompt-style experimentation, benchmark visualization, and artifact export. Through this workflow, we learn how CodeGen can be used not only as a code completion model but also as part of a structured code-generation pipeline that evaluates, filters, and organizes generated solutions. Loading the Salesforce CodeGen Model from Hugging Face python import os, sys, subprocess, textwrap, json, re, time, math, ast, tempfile, multiprocessing as mp from pathlib import Path def sh cmd : print f"\n$ {cmd}" subprocess.run cmd, shell=True, check=True sh f"{sys.executable} -m pip install -q -U transformers accelerate safetensors einops datasets evaluate pandas matplotlib tqdm rich radon tiktoken" import torch import pandas as pd import matplotlib.pyplot as plt from tqdm.auto import tqdm from rich import print from rich.panel import Panel from rich.syntax import Syntax from transformers import AutoTokenizer, AutoModelForCausalLM, set seed from radon.complexity import cc visit OUT DIR = Path "/content/codegen advanced tutorial" OUT DIR.mkdir parents=True, exist ok=True set seed 42 print Panel.fit "Salesforce CodeGen Advanced Tutorial", style="bold green" print "\nRuntime information" print "Python:", sys.version.split 0 print "Torch:", torch. version print "CUDA available:", torch.cuda.is available if torch.cuda.is available : print "GPU:", torch.cuda.get device name 0 print "CUDA memory GB:", round torch.cuda.get device properties 0 .total memory / 1e9, 2 MODEL ID = os.environ.get "CODEGEN MODEL ID", "Salesforce/codegen-350M-mono" MODEL OPTIONS = { "easy colab default": "Salesforce/codegen-350M-mono", "larger codegen1": "Salesforce/codegen-2B-mono", "codegen2 1b": "Salesforce/codegen2-1B P", "codegen25 7b mono": "Salesforce/codegen25-7b-mono P", } print "\nSelected model:", MODEL ID print "Available model examples:", MODEL OPTIONS trust remote code = any x in MODEL ID.lower for x in "codegen2", "codegen25" device = "cuda" if torch.cuda.is available else "cpu" dtype = torch.float16 if torch.cuda.is available else torch.float32 print "\nLoading tokenizer..." tokenizer = AutoTokenizer.from pretrained MODEL ID, trust remote code=trust remote code if tokenizer.pad token is None: tokenizer.pad token = tokenizer.eos token print "Loading model..." load kwargs = { "trust remote code": trust remote code, "low cpu mem usage": True, } if torch.cuda.is available : load kwargs "torch dtype" = dtype load kwargs "device map" = "auto" else: load kwargs "torch dtype" = torch.float32 model = AutoModelForCausalLM.from pretrained MODEL ID, load kwargs if not torch.cuda.is available : model.to device model.eval def count parameters model : return sum p.numel for p in model.parameters print f"Loaded {MODEL ID}" print f"Parameter count: {count parameters model /1e6:.1f}M" def generate text prompt, max new tokens=180, temperature=0.35, top p=0.92, top k=50, do sample=True, num return sequences=1, repetition penalty=1.05, : inputs = tokenizer prompt, return tensors="pt" inputs = {k: v.to model.device for k, v in inputs.items } with torch.no grad : outputs = model.generate inputs, max new tokens=max new tokens, do sample=do sample, temperature=temperature, top p=top p, top k=top k, num return sequences=num return sequences, repetition penalty=repetition penalty, pad token id=tokenizer.eos token id, eos token id=tokenizer.eos token id, decoded = tokenizer.batch decode outputs, skip special tokens=True return decoded def print code title, code : print Panel.fit title, style="bold cyan" print Syntax code, "python", theme="monokai", line numbers=True We install all required libraries and prepare the environment for running Salesforce CodeGen. We check the runtime, detect GPU availability, select the CodeGen model, and load both the tokenizer and model from Hugging Face. We also define helper functions for text generation and for displaying formatted code so that the rest of the tutorial is easier to follow. Building Extraction, Safety, and Unit-Test Validation Utilities python def extract function source full text, function name : text = full text.replace "\r\n", "\n" fence = re.search r" ?:python ?\n . ? ", text, flags=re.S | re.I if fence: text = fence.group 1 pattern = rf"^def\s+{re.escape function name }\s \ " match = re.search pattern, text, flags=re.M if not match: return "" chunk = text match.start : lines = chunk.splitlines collected = for i, line in enumerate lines : if i 0: if line.startswith "def " or line.startswith "class " : break if line.startswith "if name " : break if line and not line.startswith " ", "\t", " " and re.match r"^ A-Za-z A-Za-z0-9 \s =", line : break collected.append line source = "\n".join collected .rstrip try: ast.parse source return source except SyntaxError: fixed lines = for line in collected: fixed lines.append line candidate = "\n".join fixed lines .rstrip try: ast.parse candidate source = candidate except SyntaxError: pass return source if source.strip .startswith "def " else "" def syntax ok source : try: ast.parse source return True, "" except SyntaxError as e: return False, str e FORBIDDEN NAMES = { "eval", "exec", "compile", "open", "input", " import ", "globals", "locals", "vars", "dir", "getattr", "setattr", "delattr", "help", "breakpoint", "exit", "quit" } FORBIDDEN NODES = ast.Import, ast.ImportFrom, ast.Global, ast.Nonlocal, ast.With, ast.AsyncWith, ast.AsyncFunctionDef, ast.ClassDef, ast.Delete, ast.Raise, ALLOWED BUILTINS = { "abs": abs, "all": all, "any": any, "bool": bool, "dict": dict, "enumerate": enumerate, "float": float, "int": int, "isinstance": isinstance, "len": len, "list": list, "map": map, "max": max, "min": min, "pow": pow, "range": range, "reversed": reversed, "round": round, "set": set, "sorted": sorted, "str": str, "sum": sum, "tuple": tuple, "zip": zip, } def static safety check source : try: tree = ast.parse source except SyntaxError as e: return False, f"SyntaxError: {e}" for node in ast.walk tree : if isinstance node, FORBIDDEN NODES : return False, f"Forbidden AST node: {type node . name }" if isinstance node, ast.Name : if node.id in FORBIDDEN NAMES or node.id.startswith " " : return False, f"Forbidden name: {node.id}" if isinstance node, ast.Attribute : if node.attr.startswith " " : return False, f"Forbidden attribute: {node.attr}" if isinstance node, ast.Call : if isinstance node.func, ast.Name and node.func.id in FORBIDDEN NAMES: return False, f"Forbidden call: {node.func.id}" return True, "passed" def worker run tests source, function name, tests, queue : try: safe globals = {" builtins ": ALLOWED BUILTINS} safe locals = {} compiled = compile source, "