CS 61C Fall 2023
Extensions Staff Policies Resources Quick Links
Project 2: CS61Classify Part A
Task 1: Absolute Value (Walkthrough)
Running Tests
In this part, you will implement a few math operations that will be used for classification later. Before starting, please pull from the starter and update Venus.
$ git pull starter main
$ bash test.sh download_tools
¡ì Task 1: Absolute Value (Walkthrough)
To familiarize you with the workflow of this project, we will walk you through this task.
¡ì Running Tests
In this project, tests are written in Python and compiled into RISC-V assembly.
The Python source for the provided tests is located in unittests.py. Look over the contents of unittests.py. Although the tests are written for you in Tasks 1-4, it helps to be familiar with the unit testing framework to understand what the tests are doing.
To run the tests, on your local machine, start by running bash test.sh in the 61c-proj2 directory on your local machine. This gives you an overview of the commands you can run for testing. In particular, bash test.sh part_a compiles and runs all the tests for Part A. You can also provide the name of a specific function to compile and run all the tests for that particular function.
For this task, since we are implementing the abs function, on your local machine, run bash test.sh test_abs. This creates a test-src folder containing the Python tests compiled into RISC-V.
Since we haven’t implemented the abs function yet, some of the tests are failing. Let’s try implementing abs.
You can edit files in a text editor or directly in Venus. To edit files in Venus, switch to the Files tab. Here you can open and edit assembly files. Remember to save your files frequently with control+S (Windows) or command+S (Mac). Venus does not auto-save as you work.
Open src/abs.s (either in a text editor or Venus) and copy-paste the implementation below. abs:
blt a0, zero, done
# Negate a0
sub a0, x0, a0
Again on your local machine, run bash test.sh test_abs. The tests don’t pass, so something is probably wrong with our implementation.
¡ì Using VDB to debug tests via Venus
First, open up Venus in your web browser and mount your files. (Refer back to the setup section of the spec if you’re having trouble.)
Let’s start by setting a breakpoint. Type ebreak at the start of the abs function. This places a breakpoint just before the blt a0, zero, done instruction.
To start the debugger, in the Venus terminal, run cd /vmfs/test-src and run ls. This should list all the test files you can run. Run vdb test_abs_one.s to start the debugger for an absolute value test.
In the Venus simulator tab, Click Run to start running the program. The debugger will pause at the breakpoint we set. While paused, you can inspect the registers and memory. In particular, notice that register a0 contains the argument 1 here, because this test calls your function with argument a0 = 1.
¡ìTask2:ReLU
In this project, we will be working with integer arrays. Remember that the integers in an integer array are stored in a consecutive block of memory.
To pass an integer array as an argument, we will pass a pointer to the start of the integer array, and the number of elements in the array.
In this diagram, register a0 stores the first argument (the address of the start of the array). Register a1 stores the second argument (the number of integers in the array).
¡ì Conceptual Overview: ReLU
The ReLU function takes in an integer array and sets every negative value in the array to 0. Positive values in the array are unchanged. In other words, for each element x in the array, ReLU computes max(x, 0).
ReLU should modify the array in place. For example, if the above integer array is passed into ReLU, the result would be stored in the same place in memory:
Note that the negative values in the array were set to 0 in memory.
Using VDB to debug tests via Venus
Task 2: ReLU
Task 3: Argmax
Task 4: Dot Product Task 5: Testing Submission and Grading
Appendix: Function Definitions Appendix: Calling Convention
¡ì Your Task
Fill in the relu function in src/relu.s. relu: Task 2.
If the input is malformed in the following ways, put the appropriate return code into a0 and run j exit to quit the program. (For example, if the length of the array is less than 1, run li a0 36 and j exit.)
Return code Exception
36 The length of the array is less than 1.
¡ì Testing and debugging
To test your function, in your local terminal, run bash test.sh test_relu.
To debug your function, in your Venus terminal, run cd /vmfs/test-src, then run a VDB command to start the debugger:
vdb test_relu_standard.s
vdb test_relu_length_1.s
vdb test_relu_invalid_n.s
Here are some debugging tips that should apply to the entire project:
If you see the error “You are attempting to edit the text of the program though the program is set to immutable at address 0x00000000!”, this means that you are trying to write to memory address 0x00000000 (or whatever memory address you see in the error). This is probably happening because you’re giving this address to a store instruction, which then tries to write to this address.
If you see the error “label exit used but not defined” when starting the debugger, make sure that you’re starting the debugger with the vdb commands above. Clicking “assemble and simulate from editor” will not work.
Unfortunately the local tests don’t check for out-of-bounds memory accesses. If you ever encounter a failing test on the autograder, try making sure that your code never writes to memory outside of an array.
If your ReLU works locally but not on Gradescope, here are some edge cases we’ve seen that the local cases don’t check. You can modify unittests.py to write your own tests for these cases!
The local tests don’t perform ReLU on larger numbers. Watch the debugging videos.
¡ì Task 3: Argmax
¡ì Conceptual Overview: Argmax
The argmax function takes in an integer array and returns the index of the largest element in the array. If multiple elements are tied as the largest element, return the smallest index.
For example, if the integer array [-6, -1, 6, 1] is passed into the argmax function, the output should be 2, because the largest integer (6) is located at index 2 in the array. If the integer array were instead [6, 1, 6, 1], then the output should be 0, because the largest integer (6) is first found at index 0.
Return values None
¡ì Your Task
Fill in the argmax function in src/argmax.s. argmax: Task 3.
Return values
A pointer to the start of the integer array.
The number of integers in the array. You can assume that this argument matches the actual length of the integer array.
The index of the largest element. If the largest element appears multiple times, return the smallest index.
Return values
a4 int a0 int
Return code
The number of elements to use is less than 1. The stride of either array is less than 1.
Return values
a0 int * a1 int * a2 int a3 int * a0 int
A pointer to the start of the first input array.
A pointer to the start of the second input array.
The number of integers in the array.
A pointer to the start of the output array, where the results will be stored.
The sum of the elements in the output array. (No return value for zero-one loss.)
The size of the array to be created.
A pointer to the newly-allocated array of zeros.
initialize_zero: Task 5. Arguments a0 int Return values a0 int *
A pointer to the start of the integer array.
The number of integers in the array. You can assume that this argument matches the actual length of the integer array.
If the input is malformed in the following ways, put the appropriate return code into a0 and run j exit to quit the program. Return code Exception
36 The length of the array is less than 1.
¡ì Testing and debugging
To test your function, in your local terminal, run bash test.sh test_argmax.
To debug your function, in your Venus terminal, run cd /vmfs/test-src, then run a VDB command to start the debugger:
vdb test_argmax_invalid_n.s
vdb test_argmax_length_1.s
vdb test_argmax_standard.s
If your argmax works locally but not on Gradescope, here are some edge cases we’ve seen that the local cases don’t check. You can modify unittests.py to write your own tests for these cases!
The local tests don’t check that your code works if the largest element in the array is the last element of the array. The local tests don’t check that your code works if the largest element appears more than once.
Watch the debugging videos.
¡ì Task 4: Dot Product
¡ì Conceptual Overview: Dot Product
The dot product function takes in two integer arrays, multiplies the corresponding entries of the arrays together, and returns the sum of all the products.
For example, if these two integer arrays were passed into the dot product function, the function would return (1*6) + (2*1) + (3*6) + (4*1) + (5*6) + (6*1) + (7*6) + (8*1) + (9*6) = 170.
¡ì Conceptual Overview: Array Strides
Instead of iterating through every element of the array, what if we want to iterate through every other element, or every third element? To do this, we will define the stride of an array.
To iterate through an array with stride n, start at the beginning of the array and only consider every nth element, skipping the elements in between.
Note that the stride is given in number of elements, not number of bytes. This means that iterating with stride 1 is equivalent to iterating through every element of the array.
For example, in the above diagram, both arrays are using stride 2, so we skip every other element in the array. 5 elements should be considered, so we stop after multiplying 5 pairs of elements together. The function would return (1*6) + (3*6) + (5*6) + (7*6) + (9*6) = 150.
In the above diagram, the first array is using stride 2, so we skip every other element in this array. The second array is using stride 3, so we use every third element in this array. 3 elements should be considered, so we stop after multiplying 3 pairs of elements together. The function would return (1*6) + (3*1) + (5*6) = 39.
¡ì Your Task
Fill in the dot function in src/dot.s.
The dot function may assume that the a2 argument for the number of elements to use in the calculation will not cause an out-of-
bounds array access. However, you will need to enforce this when calling dot from other functions later in this project. dot: Task 4.
Arguments a2
A pointer to the start of the first array.
A pointer to the start of the second array.
The number of elements to use in the calculation.
The stride of the first array.
The stride of the second array.
The dot product of the two arrays, using the given number of elements and the given strides.
If the input is malformed in the following ways, put the appropriate return code into a0 and run j exit to quit the program.
¡ì Testing and debugging
To test your function, in your local terminal, run bash test.sh test_dot.
To debug your function, in your Venus terminal, run cd /vmfs/test-src, then run a VDB command to start the debugger:
vdb test_dot_length_1.s
vdb test_dot_length_error.s
vdb test_dot_length_error2.s
vdb test_dot_standard.s
vdb test_dot_stride.s
vdb test_dot_stride_error1.s
vdb test_dot_stride_error2.s
Also, check out the debugging videos!
¡ì Task 5: Testing
In this task, you will be writing tests for some mathematical functions that have already been implemented for you.
¡ì Conceptual Overview: Loss Functions
A loss function takes in two integer arrays and outputs an integer array containing some measure of how different each pair of corresponding entries are. Some loss functions also output the sum of all the difference measurements. This project uses three different loss functions.
The absolute loss function computes and outputs the absolute difference between each pair of corresponding entries, and then outputs the sum of all the absolute differences.
The squared loss function computes and outputs the square of the difference between each pair of corresponding entries, and then outputs the sum of all the squared differences.
The zero-one loss function computes whether each pair of corresponding entries is equal, and does not output any sum.
These loss functions use a helper function initialize-zero. It takes in the length of the array as input and outputs a newly- allocated array of the given length, filled with zeros.
¡ì Your Task
Fill in the tests for the three loss functions and the initialize-zero helper function in studenttests.py.
We recommend looking through unittests.py to understand how the Python framework for writing tests works.
To ensure that your tests are run by the unit testing framework, make sure that your function names start with test_! For example, the function def test_length_0(self) will run, but the function def length_0(self) will not run.
Loss functions: Task 5.
The functions will return the following error codes if the input is malformed:
Return code
The length of the array is less than 1. malloc returns an error.
To test your code coverage, run bash test.sh coverage. To get full credit on this part, make sure that your tests achieve 100% coverage. (In other words, your tests must cause every line of the implementation to be executed.)
¡ì Submission and Grading
Submit your code to the Project 2A assignment on Gradescope.
To ensure the autograder runs correctly, do not add any .import statements to the starter code. Also, make sure there are no ecall instructions in your code.
程序代写 CS代考 加QQ: 749389476