### Install Hugging Face Transformers Source: https://github.com/shailja-thakur/vgen/blob/main/VGen_Demo.ipynb Installs the Hugging Face Transformers library from its GitHub repository. This is often a prerequisite for NLP tasks. ```python !pip install git+https://github.com/huggingface/transformers.git ``` -------------------------------- ### Load Model and Tokenizer with Device Selection Source: https://github.com/shailja-thakur/vgen/blob/main/VGen_Demo.ipynb Loads the tokenizer and causal language model, selecting the appropriate device (GPU or CPU) based on availability. Ensure PyTorch and Transformers are installed. ```python if torch.cuda.is_available(): device = torch.device("cuda") print("Using GPU") else: device = torch.device("cpu") print("Using CPU") model_name = "shailja/CodeGen_2B_Verilog" tokenizer = AutoTokenizer.from_pretrained("shailja/fine-tuned-codegen-2B-Verilog") model = AutoModelForCausalLM.from_pretrained("shailja/fine-tuned-codegen-2B-Verilog").to(device) ``` -------------------------------- ### Load BigQuery Extension Source: https://github.com/shailja-thakur/vgen/blob/main/verilog-fetch-big-query.ipynb Loads the BigQuery extension for interactive environments like Jupyter notebooks. Ensure you have the google-cloud-bigquery library installed. ```python #https://stackoverflow.com/questions/36183486/importerror-no-module-named-google %load_ext google.cloud.bigquery ``` -------------------------------- ### Get Current Working Directory Source: https://github.com/shailja-thakur/vgen/blob/main/verilog-fetch-big-query.ipynb Prints the current working directory of the Python script. ```python pwd ``` -------------------------------- ### Verilog Wire Assignment Module Source: https://context7.com/shailja-thakur/vgen/llms.txt A minimal Verilog module that assigns its input directly to its output, acting as a wire. This is an example of a basic prompt. ```verilog // ── BASIC: Wire Assignment ────────────────────────────────────────────────── // prompt1_wire_assign.v (minimal comment only) // This is a module that assigns the output to the input module wire_assign( input in, output out ); // answer_wire_assign.v (reference solution) // Create a module with one input and one output that behaves like a wire module wire_assign( input in, output out ); assign out = in; endmodule ``` -------------------------------- ### Save Matplotlib Plot to SVG Source: https://github.com/shailja-thakur/vgen/blob/main/verilog-fetch-big-query.ipynb Saves the current Matplotlib plot to an SVG file with a transparent background. Ensure Matplotlib is installed and imported. ```python plt.savefig('github-language-popularity.svg',transparent=True); ``` -------------------------------- ### Save Language Popularity Plot Source: https://github.com/shailja-thakur/vgen/blob/main/verilog-fetch-big-query.ipynb This Python code snippet saves a matplotlib plot to a file named 'github-language-popularity.png'. Ensure matplotlib is installed and configured correctly. ```python plt.savefig('github-language-popularity.png'); ``` -------------------------------- ### Querying Verilog Models via Fauxpilot API Source: https://context7.com/shailja-thakur/vgen/llms.txt This Python snippet demonstrates how to use the `openai` client to interact with a local Fauxpilot server for batched Verilog completion. Ensure Fauxpilot is running and configured. ```python import openai # Point the client at the local fauxpilot server openai.api_key = "dummy" openai.api_base = "http://127.0.0.1:5000/v1" # Request 3 completions for a half-adder prompt result = openai.Completion.create( engine="codegen", prompt="//module half adder", max_tokens=100, temperature=0.1, n=3, top_p=1.0, stop=["endmodule"] ) for i, choice in enumerate(result["choices"]): print(f"--- Completion {i+1} ---") print(choice["text"] + "endmodule") ``` -------------------------------- ### Sample Verilog Code Generation with HuggingFace Source: https://github.com/shailja-thakur/vgen/blob/main/README.md Use this snippet to generate Verilog code by sampling from a fine-tuned CodeGen LLM using the HuggingFace transformers library. Ensure you have a GPU available for inference. ```python import torch from transformers import AutoTokenizer, AutoModelForCausalLM # Prompt prompt = "//module half adder " # Load model and tokenizer model_name = "shailja/CodeGen_2B_Verilog" tokenizer = AutoTokenizer.from_pretrained("shailja/fine-tuned-codegen-2B-Verilog") model = AutoModelForCausalLM.from_pretrained("shailja/fine-tuned-codegen-2B-Verilog").to(device) # Sample input_ids = tokenizer(prompt, return_tensors="pt").input_ids.to(device) sample = model.generate(input_ids, max_length=128, temperature=0.5, top_p=0.9) print(tokenizer.decode(sample[0], truncate_before_pattern=[r"endmodule"]) + "endmodule") ``` -------------------------------- ### Initialize BigQuery Client Source: https://github.com/shailja-thakur/vgen/blob/main/verilog-fetch-big-query.ipynb Initializes a BigQuery client object to interact with the BigQuery API. ```python bqclient = bigquery.Client() ``` -------------------------------- ### Sample Verilog code from prompt Source: https://github.com/shailja-thakur/vgen/blob/main/VGen_Demo_notebook.ipynb Generates Verilog code based on a given prompt using the loaded model. It decodes the generated token IDs into human-readable Verilog code, truncating before the 'endmodule' pattern. ```python prompt = "//a half adder module " # prompt="// Design a 2-to-1 multiplexer.\n module mux( \n input [4:0] a, b,\n input sel,\n output [4:0] out );\n // When sel=0, assign a to out. \n// When sel=1, assign b to out." n_steps = 1 n = 5 end_pattern='endmodule' input_ids = tokenizer(prompt, return_tensors="pt").input_ids.to(device) sample = model.generate(input_ids, max_length=128, temperature=0.5, top_p=0.9) print(tokenizer.decode(sample[0], truncate_before_pattern=[r"endmodule"]) + end_pattern) ``` -------------------------------- ### Verilog Moore State Machine - Template 2 (Some Detailed Comments) Source: https://github.com/shailja-thakur/vgen/blob/main/prompts-and-testbenches/prompts-templates.txt This template includes some detailed comments regarding state transitions and output logic, offering a balance between conciseness and clarity for the state machine's behavior. ```verilog // This is a Moore state machine with two states 0 and 1, one input in, and one output out. Reset state is 0. module simple_fsm(clk, reset, in, out); input clk; input reset; input in; output wire out; reg present_state, next_state; // Transition from state 0 to state 1 if in==0 // Transition from state 1 to state 0 if in==0 // out is high in state 0 ``` -------------------------------- ### Sample Verilog Code Generation Source: https://github.com/shailja-thakur/vgen/blob/main/VGen_Demo.ipynb Generates Verilog code by sampling from the model. This method takes a prompt and generates a completion up to a specified length, truncating before a given pattern. Requires the model and tokenizer to be loaded. ```python prompt = "//module half adder " # prompt="// Design a 2-to-1 multiplexer.\n module mux( input [4:0] a, b, input sel, output [4:0] out ); // When sel=0, assign a to out. // When sel=1, assign b to out." n_steps = 1 n = 5 end_pattern='endmodule' input_ids = tokenizer(prompt, return_tensors="pt").input_ids.to(device) # Sample sample = model.generate(input_ids, max_length=128, temperature=0.5, top_p=0.9) print(tokenizer.decode(sample[0], truncate_before_pattern=[r"endmodule"]) + end_pattern) ``` -------------------------------- ### Load Fine-Tuned CodeGen Model and Tokenizer Source: https://context7.com/shailja-thakur/vgen/llms.txt Loads a pre-trained CodeGen Verilog model and its corresponding tokenizer from HuggingFace Hub. Requires a GPU with sufficient VRAM. Select the 2B or 6B parameter model as needed. ```python import torch from transformers import AutoTokenizer, AutoModelForCausalLM # Select device device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu") print(f"Using device: {device}") # Load 2B model (use "shailja/fine-tuned-codegen-6B-Verilog" for 6B) model_name = "shailja/fine-tuned-codegen-2B-Verilog" tokenizer = AutoTokenizer.from_pretrained(model_name) model = AutoModelForCausalLM.from_pretrained(model_name).to(device) print(f"Loaded model: {model_name}") # Expected output: # Using device: cuda # Loaded model: shailja/fine-tuned-codegen-2B-Verilog ``` -------------------------------- ### Query GitHub Repositories for Verilog Files Source: https://github.com/shailja-thakur/vgen/blob/main/verilog-fetch-big-query.ipynb This query selects Verilog files from the GitHub repositories dataset, filtering for files containing 'endmodule' or 'always' and within a specific size range. It joins the files and contents tables. ```sql SELECT f.repo_name, f.path, c.copies, c.size, c.content FROM `bigquery-public-data.github_repos.files` AS f JOIN `bigquery-public-data.github_repos.contents` AS c ON f.id = c.id WHERE NOT c.binary AND ((f.path LIKE '%.v') AND (c.content LIKE '%endmodule%' OR c.content LIKE '%always%') AND (c.size BETWEEN 10 AND 1048575)) ``` -------------------------------- ### Clone fauxpilot for LLM Sampling Source: https://github.com/shailja-thakur/vgen/blob/main/README.md Clone the customized fauxpilot repository to enable sampling from fine-tuned LLMs. This method is preferred for memory and compute optimization, leading to low-latency inferencing. ```shell git clone https://github.com/shailja-thakur/fauxpilot_changes.git ``` -------------------------------- ### Import BigQuery Client Source: https://github.com/shailja-thakur/vgen/blob/main/verilog-fetch-big-query.ipynb Imports the BigQuery client library for programmatic access to BigQuery services. ```python from google.cloud import bigquery ``` -------------------------------- ### Verilog Half Adder Module Source: https://context7.com/shailja-thakur/vgen/llms.txt A Verilog module for a half adder, demonstrating an intermediate prompt style. It includes input ports 'a' and 'b', and output ports 'cout' and 'sum'. ```verilog // ── INTERMEDIATE: Half Adder ───────────────────────────────────────────────── // prompt1_half_adder.v // This is a half adder. module half_adder( input a, b, output cout, sum ); ``` -------------------------------- ### Debug Verilog generation with per-token sampling Source: https://github.com/shailja-thakur/vgen/blob/main/VGen_Demo_notebook.ipynb This snippet allows for debugging the generation process by examining the probabilities of the next token at each step. It iteratively predicts tokens, stores the top choices, and appends the most probable token to the input sequence until 'endmodule' is detected. ```python # max_length = # stop_word = iteration = dict() with torch.no_grad(): while True: iteration["Input"] = input_ids output = model(input_ids) # select logits of the first batch and the last token and apply softmax next_token_logits = output.logits[0, -1, :] next_token_probs = torch.softmax(next_token_logits, dim=-1) sorted_ids = torch.argsort(next_token_probs, dim=-1, descending=True) # print(sorted_ids) # store tokens with highest probability for choice_idx in range(n): token_id = sorted_ids[choice_idx] token_prob = next_token_probs[token_id].cpu().numpy() # print(token_id, token_prob) # print(tokenizer.decode(token_id.view(1,-1))) token_choice = ( f"{tokenizer.decode(token_id)} ({100*token_prob:.2f}%)" ) iteration[f"{choice_idx+1}"] = token_choice #Append predictions to list input_ids = torch.cat([input_ids, sorted_ids[None,0, None]], dim=-1) # condition checks on stop_words detected if 'endmodule' in tokenizer.batch_decode(input_ids)[0]: break print(tokenizer.batch_decode(input_ids)[0]) ``` -------------------------------- ### Run Verilog RTL Code Generation Inference Source: https://github.com/shailja-thakur/vgen/blob/main/README.md Use this Python code within an IPython environment to set up the OpenAI API client and perform inference for Verilog RTL code generation. Ensure the API key and base URL are correctly configured for your local VGen instance. ```python import openai openai.api_key = 'dummy' openai.api_base = 'http://127.0.0.1:5000/v1' result = openai.Completion.create(engine='codegen', prompt='//module half adder', max_tokens=100, temperature=0.1, n=3,top_p=1.0, stop=["endmodule"]) ``` -------------------------------- ### Verilog Moore State Machine - General Template Source: https://github.com/shailja-thakur/vgen/blob/main/prompts-and-testbenches/prompts-templates.txt This is a general template for a Moore state machine in Verilog, including detailed comments explaining the module's functionality and state transitions. ```verilog // This is a Moore state machine with two states 0 and 1, one input in, and one output out. Reset state is 0. module simple_fsm(clk, reset, in, out); input clk; input reset; input in; output wire out; reg present_state, next_state; // In state 0, if in=1, stay in state 0. In state 0, if in=0, go to state 1 // In state 1, if in=1, stay in state 1. In state 1, if in=0, go to state 0 // out=1 in state 0 and out=0 in state 1 ``` -------------------------------- ### Check available device info Source: https://github.com/shailja-thakur/vgen/blob/main/VGen_Demo_notebook.ipynb Determines if a CUDA-enabled GPU is available and sets the device accordingly. This is crucial for performance, as GPU acceleration is significantly faster for deep learning tasks. ```python if torch.cuda.is_available(): device = torch.device("cuda") #print("Using GPU") else: device = torch.device("cpu") #print("Using CPU") ``` -------------------------------- ### Generate Verilog Module from Natural-Language Prompt Source: https://context7.com/shailja-thakur/vgen/llms.txt Generates Verilog code from a natural-language prompt using a fine-tuned CodeGen model. Uses temperature sampling and stops generation at the 'endmodule' keyword. Ensure the prompt is in a comment style for optimal results. ```python import torch from transformers import AutoTokenizer, AutoModelForCausalLM device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu") tokenizer = AutoTokenizer.from_pretrained("shailja/fine-tuned-codegen-2B-Verilog") model = AutoModelForCausalLM.from_pretrained("shailja/fine-tuned-codegen-2B-Verilog").to(device) # Natural-language prompt (comment style used during fine-tuning) prompt = "//module half adder " input_ids = tokenizer(prompt, return_tensors="pt").input_ids.to(device) # Generate up to 128 tokens sample = model.generate(input_ids, max_length=128, temperature=0.5, top_p=0.9) result = tokenizer.decode(sample[0], truncate_before_pattern=[r"endmodule"]) + "endmodule" print(result) # Expected output: # //module half adder # # module half_adder( # input a, # input b, # output sum, # output carry # ); # # assign sum = a ^ b; # assign carry = a & b; # # endmodule ``` -------------------------------- ### Query Stack Overflow for Verilog Questions Source: https://github.com/shailja-thakur/vgen/blob/main/verilog-fetch-big-query.ipynb This SQL query retrieves questions tagged with 'Verilog' from the Stack Overflow dataset, including their titles, bodies, and associated answers. ```sql select q.tags question_tag, q.id question_id, q.title question_title, q.body question_body, a.id answer_id, a.body from `bigquery-public-data.stackoverflow.posts_questions` as q left join `bigquery-public-data.stackoverflow.posts_answers` as a on a.parent_id = q.id where regexp_contains(q.tags, 'Verilog'); ``` -------------------------------- ### Load model and tokenizer Source: https://github.com/shailja-thakur/vgen/blob/main/VGen_Demo_notebook.ipynb Loads a pre-trained CodeGen model and its corresponding tokenizer from Hugging Face. The model is then moved to the determined device (GPU or CPU). ```python model_name = "shailja/CodeGen_6B_Verilog" tokenizer = AutoTokenizer.from_pretrained("shailja/fine-tuned-codegen-6B-Verilog") model = AutoModelForCausalLM.from_pretrained("shailja/fine-tuned-codegen-6B-Verilog").to(device) ``` -------------------------------- ### Set Google Cloud Credentials Source: https://github.com/shailja-thakur/vgen/blob/main/verilog-fetch-big-query.ipynb Sets the environment variable for Google Cloud credentials. Replace 'my-project-348716-29f69b900233.json' with your actual service account key file path. ```python #Your credentials to google cloud os.environ["GOOGLE_APPLICATION_CREDENTIALS"]=r"my-project-348716-29f69b900233.json" # git token TOKEN = "" # Replace with your token ``` -------------------------------- ### Plot GitHub Language Popularity (Verilog Focus) Source: https://github.com/shailja-thakur/vgen/blob/main/verilog-fetch-big-query.ipynb Generates a bar chart showing the popularity of programming languages from GitHub data, with a specific emphasis and annotation for Verilog. Requires pre-existing sorted_Languages_counts and total_count variables. ```python total_count = sum(count for _, count in sorted_Languages_counts) # Find the index of Verilog verilog_index = next(i for i, v in enumerate(sorted_Languages_counts) if v[0] == 'Verilog') # Number of initial languages to display initial_display_count = 5 # Create a new list with the first few languages, placeholder, and Verilog selected_languages_counts = sorted_Languages_counts[:initial_display_count] + [('...', 0), sorted_Languages_counts[verilog_index]] # Unpack languages and counts language, count = zip(*selected_languages_counts) x_pos = np.arange(len(language)) # Plot plt.figure(figsize=(7,5)) bars = plt.bar(x_pos, count, align='center', color=sns.color_palette("gist_rainbow", len(x_pos))) plt.xticks(x_pos, language, rotation=15, fontsize=25) # Annotate bars with percentage for idx, bar in enumerate(bars): height = bar.get_height() percentage = (height / total_count) * 100 if language[idx] == 'Verilog': # Highlight Verilog plt.text(bar.get_x() + bar.get_width() / 2, height, f'{percentage:.2f}%', ha='center', va='bottom', fontsize=20, fontweight='bold', color='red') elif language[idx] != '...': # Avoid annotating '...' plt.text(bar.get_x() + bar.get_width() / 2, height, f'{percentage:.2f}%', ha='center', va='bottom', fontsize=15) plt.show() ``` -------------------------------- ### Import necessary libraries Source: https://github.com/shailja-thakur/vgen/blob/main/VGen_Demo_notebook.ipynb Imports the PyTorch library for tensor operations and AutoTokenizer and AutoModelForCausalLM from Hugging Face Transformers for model and tokenizer handling. ```python import torch from transformers import AutoTokenizer, AutoModelForCausalLM ``` -------------------------------- ### Per-Token Greedy Decoding for Verilog Generation Source: https://context7.com/shailja-thakur/vgen/llms.txt This Python snippet performs greedy decoding one token at a time, recording top candidate tokens and their probabilities. Generation stops when 'endmodule' is encountered. Requires `transformers` and `torch` libraries. ```python import torch from transformers import AutoTokenizer, AutoModelForCausalLM device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu") tokenizer = AutoTokenizer.from_pretrained("shailja/fine-tuned-codegen-6B-Verilog") model = AutoModelForCausalLM.from_pretrained("shailja/fine-tuned-codegen-6B-Verilog").to(device) prompt = "//a half adder module " n = 5 # number of top token candidates to track input_ids = tokenizer(prompt, return_tensors="pt").input_ids.to(device) iteration = dict() with torch.no_grad(): while True: output = model(input_ids) next_token_logits = output.logits[0, -1, :] next_token_probs = torch.softmax(next_token_logits, dim=-1) sorted_ids = torch.argsort(next_token_probs, dim=-1, descending=True) for choice_idx in range(n): token_id = sorted_ids[choice_idx] token_prob = next_token_probs[token_id].cpu().numpy() iteration[f"{choice_idx+1}"] = ( f"{tokenizer.decode(token_id)} ({100 * token_prob:.2f}%)" ) # Append the greedy best token input_ids = torch.cat([input_ids, sorted_ids[None, 0, None]], dim=-1) if "endmodule" in tokenizer.batch_decode(input_ids)[0]: break print(tokenizer.batch_decode(input_ids)[0]) ``` -------------------------------- ### Prepare Data for Plotting Source: https://github.com/shailja-thakur/vgen/blob/main/verilog-fetch-big-query.ipynb Prepares language and count data for plotting the top 10 programming languages. Requires numpy, matplotlib, and seaborn libraries. ```python import numpy as np import matplotlib.pyplot as plt import seaborn as sns language = list(zip(*sorted_Languages_counts[:10]))[0] count = list(zip(*sorted_Languages_counts[:10]))[1] x_pos = np.arange(len(language)) ``` -------------------------------- ### SQL Query for Verilog Answers Source: https://github.com/shailja-thakur/vgen/blob/main/verilog-fetch-big-query.ipynb Constructs a SQL query to retrieve answers containing 'Verilog' from the Stack Overflow dataset. This query joins questions and answers tables and filters by answer body content. ```sql query_string = """ SELECT p.id answer_id, p.body FROM `bigquery-public-data.stackoverflow.posts_answers` as p WHERE p.body LIKE '%Verilog%' """ # where regexp_contains(q.tags, 'Verilog') ``` -------------------------------- ### BigQuery SQL Query for Stack Overflow Verilog Questions Source: https://github.com/shailja-thakur/vgen/blob/main/verilog-fetch-big-query.ipynb Constructs a SQL query to retrieve URLs and view counts for Stack Overflow questions tagged with 'verilog' or containing specific Verilog keywords in their tags. This query is intended for use with Google BigQuery. ```sql SELECT CONCAT( 'https://stackoverflow.com/questions/', CAST(id as STRING)) as url, view_count FROM `bigquery-public-data.stackoverflow.posts_questions` WHERE ((tags like '%verilog%') or (tags like '%beginmodule%' and tags like '%endmodule%' and tags like '%always%') ) ORDER BY view_count DESC ``` -------------------------------- ### Verilog Signed Addition with Overflow Detection Module Source: https://context7.com/shailja-thakur/vgen/llms.txt An advanced Verilog module for signed addition of two 8-bit 2's complement numbers, including overflow detection. This uses an advanced prompt style. ```verilog // ── ADVANCED: Signed Addition with Overflow Detection ─────────────────────── // prompt1_signed-addition-overflow.v // This is a signed adder that adds two 8-bit 2's complement numbers. // It also captures a signed overflow. module signed_adder(input [7:0] a, input [7:0] b, output [7:0] s, output overflow ); ``` -------------------------------- ### Query GitHub Languages Source: https://github.com/shailja-thakur/vgen/blob/main/verilog-fetch-big-query.ipynb This query selects the 'language' field from the GitHub repositories dataset. The 'language' field is a list of dictionaries, each containing a language name and its byte count. ```sql SELECT language FROM `bigquery-public-data.github_repos.languages` ``` -------------------------------- ### Import Libraries Source: https://github.com/shailja-thakur/vgen/blob/main/verilog-fetch-big-query.ipynb Imports necessary libraries for data manipulation and interacting with the operating system. ```python import os import pandas as pd ``` -------------------------------- ### Token-by-Token Verilog Code Generation Source: https://github.com/shailja-thakur/vgen/blob/main/VGen_Demo.ipynb Generates Verilog code one token at a time, selecting the most probable next token. This method allows for more control and inspection of the generation process. It requires the model and tokenizer to be loaded and operates within a `torch.no_grad()` context. ```python iteration = dict() with torch.no_grad(): while True: iteration["Input"] = input_ids output = model(input_ids) # select logits of the first batch and the last token and apply softmax next_token_logits = output.logits[0, -1, :] next_token_probs = torch.softmax(next_token_logits, dim=-1) sorted_ids = torch.argsort(next_token_probs, dim=-1, descending=True) # print(sorted_ids) # store tokens with highest probability for choice_idx in range(n): token_id = sorted_ids[choice_idx] token_prob = next_token_probs[token_id].cpu().numpy() # print(token_id, token_prob) # print(tokenizer.decode(token_id.view(1,-1))) token_choice = ( f"{tokenizer.decode(token_id)} ({100*token_prob:.2f}%)" ) iteration[f"{choice_idx+1}"] = token_choice #Append predictions to list input_ids = torch.cat([input_ids, sorted_ids[None,0, None]], dim=-1) # condition checks on stop_words detected if 'endmodule' in tokenizer.batch_decode(input_ids)[0]: break print(tokenizer.batch_decode(input_ids)[0]) ``` -------------------------------- ### Check GPU Availability Source: https://github.com/shailja-thakur/vgen/blob/main/VGen_Demo.ipynb Checks if a GPU is connected and available for use. It prints the GPU information if connected, otherwise indicates that no GPU is found. ```python gpu_info = !nvidia-smi gpu_info = '\n'.join(gpu_info) if gpu_info.find('failed') >= 0: print('Not connected to a GPU') else: print(gpu_info) ``` -------------------------------- ### Generate Plagiarism Pie Chart Source: https://github.com/shailja-thakur/vgen/blob/main/verilog-fetch-big-query.ipynb Creates a pie chart to visualize the proportion of 'Unique' and 'Plagiarised' content, with 'Plagiarised' highlighted. The chart is saved as a PNG file. Note: The `plt.show()` call is commented out. ```python import matplotlib.pyplot as plt explode = (0, 0.1) # only "explode" the 2nd slice (i.e. 'Hogs') labels = 'Unique', 'Plagiarised' sizes = [15, 30] fig = plt.gcf() fig, ax = plt.subplots() plt.figure(figsize=(50,50)) ax.pie(sizes, explode=explode, labels=labels,colors=['green', 'red'], autopct='%1.1f%%', shadow=True, startangle=90,wedgeprops={'alpha':0.6}) # plt.savefig('pie_plagiarism.svg',transparent=True,bbox_inches='tight', format="svg") plt.savefig('pie_plagiarism.png',transparent=True,bbox_inches='tight') # plt.show() ``` -------------------------------- ### Verilog Moore State Machine - Template 1 (Least Detailed) Source: https://github.com/shailja-thakur/vgen/blob/main/prompts-and-testbenches/prompts-templates.txt This template represents a Moore state machine with minimal comments after the internal signal declarations. It is suitable when detailed inline explanations are not required. ```verilog // This is a Moore state machine with two states 0 and 1, one input in, and one output out. Reset state is 0. module simple_fsm(clk, reset, in, out); input clk; input reset; input in; output wire out; reg present_state, next_state; ``` -------------------------------- ### Update and Plot GitHub Language Popularity (JS Alias) Source: https://github.com/shailja-thakur/vgen/blob/main/verilog-fetch-big-query.ipynb Updates language names (e.g., 'JavaScript' to 'JS') and then plots language popularity, highlighting Verilog and saving the plot to SVG and PDF. Assumes sorted_Languages_counts and total_count are defined. ```python # Updating the data to reflect the change for JavaScript to JS updated_sorted_Languages_counts = [(('JS' if lang == 'JavaScript' else lang), count) for lang, count in sorted_Languages_counts] # Find the index of Verilog in the updated list verilog_index = next(i for i, v in enumerate(updated_sorted_Languages_counts) if v[0] == 'Verilog') # Number of initial languages to display initial_display_count = 5 # Create a new list with the first few languages, placeholder, and Verilog selected_languages_counts = updated_sorted_Languages_counts[:initial_display_count] + [('...', 0), updated_sorted_Languages_counts[verilog_index]] # Unpack languages and counts language, count = zip(*selected_languages_counts) x_pos = np.arange(len(language)) # Plot plt.figure(figsize=(6,3)) bars = plt.bar(x_pos, count, align='center', color=sns.color_palette("gist_rainbow", len(x_pos))) # Annotate bars with percentage for idx, bar in enumerate(bars): height = bar.get_height() percentage = (height / total_count) * 100 if language[idx] == 'Verilog': # Highlight Verilog plt.text(bar.get_x() + bar.get_width() / 2, height, f'{percentage:.2f}%', ha='center', va='bottom', fontsize=20, fontweight='bold', color='red') elif language[idx] != '...': # Avoid annotating '...' plt.text(bar.get_x() + bar.get_width() / 2, height, f'{percentage:.2f}%', ha='center', va='bottom', fontsize=13) plt.yticks( fontsize=15) plt.xticks(x_pos, language, rotation=30, fontsize=15) plt.savefig('github-language-popularity.svg', bbox_inches='tight'); plt.savefig('github-language-popularity.pdf'); plt.show() ``` -------------------------------- ### Verilog Testbench for Advanced FSM (Pattern 101 Detector) Source: https://context7.com/shailja-thakur/vgen/llms.txt A Verilog testbench (`tb_adv_fsm.v`) for evaluating an advanced Finite State Machine (FSM) that detects the pattern '101'. It includes clock generation, reset sequence, and stimulus application to verify state transitions and output. ```verilog // tb_adv_fsm.v — testbench for the advanced FSM (pattern 101 detector) `timescale 1 ns/10 ps module tb_adv_fsm; reg clk, reset, x; wire z; localparam period = 20; adv_fsm UUT (.clk(clk), .reset(reset), .x(x), .z(z)); initial begin clk = 0; forever #(period/2) clk = ~clk; end initial begin #2; reset = 1; x = 0; #period; if (z !== 0) begin $display("test 1 failed"); $finish; end reset = 0; x = 0; #period; // IDLE if (z !== 0) begin $display("test 2 failed"); $finish; end x = 1; #period; // → S1 if (z !== 0) begin $display("test 3 failed"); $finish; end x = 0; #period; // → S2 if (z !== 0) begin $display("test 4 failed"); $finish; end x = 1; #period; // → S3, output = 1 if (z !== 1) begin $display("test 5 failed"); $finish; end #period; // → back to S1 if (z !== 0) begin $display("test 6 failed"); $finish; end $display("all tests passed"); $finish; end endmodule ``` -------------------------------- ### Prepare Data for Plotting Source: https://github.com/shailja-thakur/vgen/blob/main/verilog-fetch-big-query.ipynb Extracts language names and their counts from a sorted list of tuples, preparing them for visualization. Requires numpy and matplotlib. ```python language = list(zip(*sorted_Languages_counts[:155]))[0] count = list(zip(*sorted_Languages_counts[:155]))[1] x_pos = np.arange(len(language)) plt.figure(figsize=(12,8)) ``` -------------------------------- ### Python PDF Extraction Utilities Source: https://context7.com/shailja-thakur/vgen/llms.txt Python code snippet demonstrating the use of `pdf_extractor` library functions (`readPDF`, `getTotalPages`, `getPages`) to extract text from Verilog PDF textbooks. This is useful for building training corpora. ```python from pdf_extractor import readPDF, getTotalPages, getPages, preprocess # Open a Verilog textbook PDF pdf = readPDF("verilog_textbook.pdf") # Expected output: # Document Metadata # {'/Title': 'Digital Design with Verilog', '/Author': '...'} total = getTotalPages(pdf) # Expected output: # Total pages: 412 # Extract all text all_text = getPages(pdf, total) print(all_text[:200]) # Expected output (raw extracted text): ``` -------------------------------- ### Plotting Language Popularity with Matplotlib Source: https://github.com/shailja-thakur/vgen/blob/main/verilog-fetch-big-query.ipynb Generates a bar chart to visualize language popularity scores. Customize x-axis ticks for better readability. ```python plt.bar(x_pos, count,align='center',color=sns.color_palette("gist_rainbow",len(x_pos))) plt.xticks(x_pos, language,rotation=30, fontsize=25) ``` -------------------------------- ### Verilog 5-bit Galois LFSR Module Source: https://context7.com/shailja-thakur/vgen/llms.txt A Verilog module for a 5-bit maximal-length Galois Linear Feedback Shift Register (LFSR) with specified taps. This represents an intermediate prompt. ```verilog // ── INTERMEDIATE: 5-bit Galois LFSR ───────────────────────────────────────── // prompt1_lfsr.v // This is a 5-bit maximal-length Galois LFSR with taps at bit positions 5 and 3 module lfsr( input clk, input reset, output [4:0] q ); reg [4:0] r_reg; wire [4:0] r_next; wire feedback_value; ``` -------------------------------- ### Verilog Testbench for Wire Assignment Source: https://context7.com/shailja-thakur/vgen/llms.txt A Verilog testbench (`tb_wire_assign.v`) designed to functionally evaluate the `wire_assign` module. It applies input stimuli and checks the output against expected values, reporting pass/fail messages. ```verilog // tb_wire_assign.v — testbench for the wire_assign module `timescale 1 ns/10 ps module tb_wire_assign; reg in; wire out; localparam period = 2; wire_assign UUT (.in(in), .out(out)); initial begin in = 0; #period; if (out !== 0) begin $display("test 1 failed"); $finish; end else $display("in = %b , out = %b", in, out); in = 1; #period; if (out !== 1) begin $display("test 2 failed"); $finish; end else $display("in = %b , out = %b", in, out); in = 0; #period; if (out !== 0) begin $display("test 3 failed"); $finish; end else $display("in = %b , out = %b", in, out); $display("all tests passed"); $finish; end endmodule ``` -------------------------------- ### Verilog Moore FSM (Pattern 101 Detector) Module Source: https://context7.com/shailja-thakur/vgen/llms.txt An advanced Verilog module implementing a Moore state machine that detects the pattern '101'. It includes clock, reset, input, and output signals, with state registers. ```verilog // ── ADVANCED: Moore FSM (pattern 101 detector) ─────────────────────────────── // prompt1_simple-fsm.v // This is a Moore state machine with two states 0 and 1, one input in, and one output out. // Reset state is 0. Output is high in state 0. If in is low, state changes. module simple_fsm(input clk, input reset, input in, output out); reg present_state, next_state; ``` -------------------------------- ### Print Sorted Language Counts Source: https://github.com/shailja-thakur/vgen/blob/main/verilog-fetch-big-query.ipynb This Python code prints a list of tuples, where each tuple contains a programming language and its corresponding count. This is useful for displaying the results of language popularity analysis. ```python print(sorted_Languages_counts) ``` -------------------------------- ### Sort and Display Language Counts Source: https://github.com/shailja-thakur/vgen/blob/main/verilog-fetch-big-query.ipynb This Python code sorts the programming language counts in descending order and displays the top 200. It uses the 'operator' module for sorting. ```python import operator sorted_Languages_counts = sorted(Languages_count.items(), key=operator.itemgetter(1),reverse=True) sorted_Languages_counts[:200] ``` -------------------------------- ### Process and Count Programming Languages Source: https://github.com/shailja-thakur/vgen/blob/main/verilog-fetch-big-query.ipynb This Python code iterates through a list of language data, flattens the nested structure, and counts the occurrences of each programming language. It's useful for analyzing the distribution of languages in a dataset. ```python languagesList=[] for lang in dataframe.language: languagesList.extend(lang) ``` ```python Languages_count={} for lang in languagesList: if lang["name"] not in Languages_count: Languages_count[lang["name"]]=0 Languages_count[lang["name"]]+=1 #Languages_count ``` -------------------------------- ### Save DataFrame to CSV Source: https://github.com/shailja-thakur/vgen/blob/main/verilog-fetch-big-query.ipynb Saves the current DataFrame to a CSV file. Ensure the file path is correctly specified for your environment. ```python dataframe.to_csv( '/Volumes/GoogleDrive/My Drive/sg0_workspace/datasets/verilog_github_contains_always_module_multicopies.csv',index=False) ``` ```python dataframe.to_csv( '/Volumes/GoogleDrive/My Drive/sg0_workspace/datasets/verilog_github_contains_always_module.csv',index=False) ``` ```python dataframe.to_csv( '/Volumes/GoogleDrive/My Drive/sg0_workspace/datasets/verilog_github_contains_always_or_endmodule.csv',index=False) ``` -------------------------------- ### Calculate Trend Line and Plot Language Popularity Source: https://github.com/shailja-thakur/vgen/blob/main/verilog-fetch-big-query.ipynb Calculates the linear trend line for language popularity scores and generates a horizontal bar chart. Requires numpy, matplotlib, and seaborn libraries. ```python slope, intercept = np.polyfit(x_pos, count, 1) trendline = intercept + (slope * x_pos) plt.figure(figsize=(10,30)) # plt.plot(x_pos, trendline, color='black', linestyle='--') plt.barh(x_pos, count,align='center',color=sns.color_palette("gist_rainbow",len(x_pos))) plt.xticks(x_pos, language,rotation=45) ``` -------------------------------- ### Define and Execute BigQuery Query Function Source: https://github.com/shailja-thakur/vgen/blob/main/verilog-fetch-big-query.ipynb A Python function to execute a BigQuery SQL query and convert the results into a pandas DataFrame. It optionally uses the BigQuery Storage API for faster data retrieval. ```python def query(query_string): dataframe = ( bqclient.query(query_string) .result() .to_dataframe( # Optionally, explicitly request to use the BigQuery Storage API. As of # google-cloud-bigquery version 1.26.0 and above, the BigQuery Storage # API is used by default. create_bqstorage_client=True, ) ) print(dataframe.head()) return dataframe ``` -------------------------------- ### Text Labels for Plot Source: https://github.com/shailja-thakur/vgen/blob/main/verilog-fetch-big-query.ipynb Defines text labels for various programming languages to be used in plots. This is typically part of a larger plotting script. ```python Text(131, 0, 'HLSL'), Text(132, 0, 'Rebol'), Text(133, 0, 'AutoHotkey'), Text(134, 0, 'SAS'), Text(135, 0, 'LLVM'), Text(136, 0, 'Module Management System'), Text(137, 0, 'Procfile'), Text(138, 0, 'Starlark'), Text(139, 0, 'ShaderLab'), Text(140, 0, 'Crystal'), Text(141, 0, 'Meson'), Text(142, 0, 'XQuery'), Text(143, 0, 'Jinja'), Text(144, 0, 'KiCad') ``` -------------------------------- ### Plot Result Display Source: https://github.com/shailja-thakur/vgen/blob/main/verilog-fetch-big-query.ipynb Represents the output of a plotting function, indicating the figure size and axes. This is a representation of a Matplotlib figure object. ```python
```