-
Notifications
You must be signed in to change notification settings - Fork 28
initial implementation for logistic_regression #293
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Jingzhou-Qiu
wants to merge
7
commits into
UVA-LavaLab:main
Choose a base branch
from
Jingzhou-Qiu:logistic_regression
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d46cf2a
initial implementation for logistic_regression
Jingzhou-Qiu 8317cfa
fix some issues
Jingzhou-Qiu 592f11e
add GPU implementation
Jingzhou-Qiu 9ed1eaa
add gpu implementation
Jingzhou-Qiu a6392a4
make small changes
Jingzhou-Qiu 2f535b5
add slurm script
Jingzhou-Qiu 1d56a47
debugging for very large input
Jingzhou-Qiu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # Makefile: C++ version of logistic regression | ||
| # Copyright (c) 2024 University of Virginia | ||
| # This file is licensed under the MIT License. | ||
| # See the LICENSE file in the root of this repository for more details. | ||
|
|
||
| SUBDIRS := PIM | ||
|
|
||
| .PHONY: debug perf dramsim3_integ clean $(SUBDIRS) | ||
| .DEFAULT_GOAL := perf | ||
|
|
||
| USE_OPENMP ?= 0 | ||
|
|
||
| debug perf dramsim3_integ clean: $(SUBDIRS) | ||
|
|
||
| $(SUBDIRS): | ||
| $(MAKE) -C $@ $(MAKECMDGOALS) USE_OPENMP=$(USE_OPENMP) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # Makefile: C++ version of logistic regression | ||
| # Copyright (c) 2024 University of Virginia | ||
| # This file is licensed under the MIT License. | ||
| # See the LICENSE file in the root of this repository for more details. | ||
|
|
||
| PROJ_ROOT = ../../.. | ||
| include ${PROJ_ROOT}/Makefile.common | ||
|
|
||
| # make USE_OPENMP=1 | ||
| USE_OPENMP ?= 0 | ||
| ifeq ($(USE_OPENMP),1) | ||
| CXXFLAGS += -fopenmp | ||
| endif | ||
|
|
||
| EXEC := lr.out | ||
| SRC := lr.cpp | ||
|
|
||
| debug perf dramsim3_integ: $(EXEC) | ||
|
|
||
| $(EXEC): $(SRC) $(DEPS) | ||
| $(CXX) $< $(CXXFLAGS) -o $@ | ||
|
|
||
| clean: | ||
| rm -rf $(EXEC) *.dSYM |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,268 @@ | ||
| #include <iostream> | ||
| #include <vector> | ||
| #include <getopt.h> | ||
| #include <stdint.h> | ||
| #include <cmath> | ||
| #include <chrono> | ||
| #include <iomanip> | ||
| #if defined(_OPENMP) | ||
| #include <omp.h> | ||
| #endif | ||
|
|
||
| #include "util.h" | ||
| #include "libpimeval.h" | ||
|
|
||
| using namespace std; | ||
|
|
||
| struct Params { | ||
| uint64_t dataSize = 2048; | ||
| int epochs = 1000; | ||
| float learningRate = 0.01f; | ||
| char* configFile = nullptr; | ||
| char* inputFile = nullptr; | ||
| bool shouldVerify = true; | ||
| }; | ||
|
|
||
| void usage() { | ||
| fprintf(stderr, | ||
| "\nUsage: ./lr.out [options]" | ||
| "\n" | ||
| "\n -l input size (default=2048 elements)" | ||
| "\n -e number of epochs (default=1000)" | ||
| "\n -r learning rate (default=0.01)" | ||
| "\n -c DRAMsim config file" | ||
| "\n -i input file (not implemented)" | ||
| "\n -v t = verify with host output" | ||
| "\n"); | ||
| } | ||
|
|
||
| Params getInputParams(int argc, char** argv) { | ||
| Params p; | ||
| int opt; | ||
| while ((opt = getopt(argc, argv, "h:l:e:r:c:i:v:")) >= 0) { | ||
| switch (opt) { | ||
| case 'h': | ||
| usage(); | ||
| exit(0); | ||
| case 'l': | ||
| p.dataSize = strtoull(optarg, nullptr, 0); | ||
| break; | ||
| case 'e': | ||
| p.epochs = atoi(optarg); | ||
| break; | ||
| case 'r': | ||
| p.learningRate = atof(optarg); | ||
| break; | ||
| case 'c': | ||
| p.configFile = optarg; | ||
| break; | ||
| case 'i': | ||
| p.inputFile = optarg; | ||
| break; | ||
| case 'v': | ||
| p.shouldVerify = (*optarg == 't'); | ||
| break; | ||
| default: | ||
| fprintf(stderr, "\nUnrecognized option!\n"); | ||
| usage(); | ||
| exit(0); | ||
| } | ||
| } | ||
| return p; | ||
| } | ||
|
|
||
| float sigmoid_exact(float z) { | ||
| return 1.0f / (1.0f + exp(-z)); | ||
| } | ||
|
|
||
| void runLogisticRegressionPIM(uint64_t dataSize, int epochs, float lr, const vector<int>& X, const vector<int>& Y, float& w, float& b) { | ||
| PimObjId xObj = pimAlloc(PIM_ALLOC_AUTO, dataSize, PIM_FP32); | ||
| if (xObj == -1) | ||
| { | ||
| std::cout << "Abort" << std::endl; | ||
| return; | ||
| } | ||
| PimObjId yObj = pimAllocAssociated(xObj, PIM_FP32); | ||
| if (yObj == -1) | ||
| { | ||
| std::cout << "Abort" << std::endl; | ||
| return; | ||
| } | ||
| PimObjId predictionObj = pimAllocAssociated(xObj, PIM_FP32); | ||
| if (predictionObj == -1) | ||
| { | ||
| std::cout << "Abort" << std::endl; | ||
| return; | ||
| } | ||
| PimObjId errorObj = pimAllocAssociated(xObj, PIM_FP32); | ||
| if (errorObj == -1) | ||
| { | ||
| std::cout << "Abort" << std::endl; | ||
| return; | ||
| } | ||
|
|
||
| std::vector<float> Xf(dataSize), Yf(dataSize); | ||
| for (uint64_t i = 0; i < dataSize; ++i) { | ||
| Xf[i] = static_cast<float>(X[i]); | ||
| Yf[i] = static_cast<float>(Y[i]); | ||
| } | ||
|
|
||
| PimStatus status = pimCopyHostToDevice(Xf.data(), xObj); | ||
| if (status != PIM_OK) | ||
| { | ||
| std::cout << "Abort" << std::endl; | ||
| return; | ||
| } | ||
|
|
||
| status = pimCopyHostToDevice(Yf.data(), yObj); | ||
| if (status != PIM_OK) | ||
| { | ||
| std::cout << "Abort" << std::endl; | ||
| return; | ||
| } | ||
|
|
||
| std::vector<float> zBuffer(dataSize); | ||
|
|
||
| for (int epoch = 0; epoch < epochs; ++epoch) { | ||
| float dw = 0.0f, db = 0.0f; | ||
|
|
||
| status = pimMulScalar(xObj, predictionObj, *(uint64_t*)&w); | ||
fasiddique marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (status != PIM_OK) | ||
| { | ||
| std::cout << "Abort" << std::endl; | ||
| return; | ||
| } | ||
|
|
||
| status = pimAddScalar(predictionObj, predictionObj, *(uint64_t*)&b); | ||
| if (status != PIM_OK) | ||
| { | ||
| std::cout << "Abort" << std::endl; | ||
| return; | ||
| } | ||
| status = pimCopyDeviceToHost(predictionObj, zBuffer.data()); | ||
| if (status != PIM_OK) | ||
| { | ||
| std::cout << "Abort" << std::endl; | ||
| return; | ||
| } | ||
|
|
||
| for (uint64_t i = 0; i < dataSize; ++i) { | ||
| zBuffer[i] = sigmoid_exact(zBuffer[i]); | ||
| } | ||
fasiddique marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| status = pimCopyHostToDevice(zBuffer.data(), predictionObj); | ||
| if (status != PIM_OK) | ||
| { | ||
| std::cout << "Abort" << std::endl; | ||
| return; | ||
| } | ||
|
|
||
| status = pimSub(predictionObj, yObj, errorObj); | ||
| if (status != PIM_OK) | ||
| { | ||
| std::cout << "Abort" << std::endl; | ||
| return; | ||
| } | ||
|
|
||
| status = pimMul(errorObj, xObj, predictionObj); | ||
| if (status != PIM_OK) | ||
| { | ||
| std::cout << "Abort" << std::endl; | ||
| return; | ||
| } | ||
|
|
||
| status = pimRedSum(predictionObj, &dw); | ||
| if (status != PIM_OK) | ||
| { | ||
| std::cout << "Abort" << std::endl; | ||
| return; | ||
| } | ||
|
|
||
| status = pimRedSum(errorObj, &db); | ||
| if (status != PIM_OK) | ||
| { | ||
| std::cout << "Abort" << std::endl; | ||
| return; | ||
| } | ||
|
|
||
| w -= lr * dw / dataSize; | ||
| b -= lr * db / dataSize; | ||
| } | ||
|
|
||
| pimFree(xObj); | ||
| pimFree(yObj); | ||
| pimFree(predictionObj); | ||
| pimFree(errorObj); | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
| void runLogisticRegressionHost(uint64_t n, int epochs, float lr, const vector<int>& X, const vector<int>& Y, float& w_host, float& b_host) { | ||
| vector<float> Xf(n), Yf(n); | ||
| for (uint64_t i = 0; i < n; ++i) { | ||
| Xf[i] = static_cast<float>(X[i]); | ||
| Yf[i] = static_cast<float>(Y[i]); | ||
| } | ||
|
|
||
| w_host = 0.0f; | ||
| b_host = 0.0f; | ||
|
|
||
| for (int epoch = 0; epoch < epochs; ++epoch) { | ||
| float dw = 0.0f, db = 0.0f; | ||
| for (uint64_t i = 0; i < n; ++i) { | ||
| float z = w_host * Xf[i] + b_host; | ||
| float pred = sigmoid_exact(z); | ||
| float error = pred - Yf[i]; | ||
| dw += error * Xf[i]; | ||
| db += error; | ||
| } | ||
| w_host -= lr * dw / n; | ||
| b_host -= lr * db / n; | ||
| } | ||
| } | ||
|
|
||
| int main(int argc, char* argv[]) { | ||
| Params params = getInputParams(argc, argv); | ||
|
|
||
| if (!createDevice(params.configFile)) return 1; | ||
Jingzhou-Qiu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| vector<int> X(params.dataSize), Y(params.dataSize); | ||
Jingzhou-Qiu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (params.shouldVerify) { | ||
| getVector(params.dataSize, X); | ||
| getVector(params.dataSize, Y); | ||
| for (auto& y : Y) y = y % 2; | ||
fasiddique marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| float w = 0.0f, b = 0.0f; | ||
|
|
||
| auto start = chrono::high_resolution_clock::now(); | ||
| runLogisticRegressionPIM(params.dataSize, params.epochs, params.learningRate, X, Y, w, b); | ||
| auto end = chrono::high_resolution_clock::now(); | ||
Jingzhou-Qiu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| pimShowStats(); | ||
|
|
||
| chrono::duration<double, milli> elapsed = end - start; | ||
| cout << "Duration: " << fixed << setprecision(3) << elapsed.count() << " ms\n"; | ||
| cout << "Model: sigmoid(" << w << " * x + " << b << ")\n"; | ||
|
|
||
|
|
||
| if (params.shouldVerify) { | ||
| float w_host, b_host; | ||
| runLogisticRegressionHost(params.dataSize, params.epochs, params.learningRate, X, Y, w_host, b_host); | ||
|
|
||
| cout << "Host Model: sigmoid(" << w_host << " * x + " << b_host << ")\n"; | ||
|
|
||
| float w_diff = fabs(w - w_host); | ||
| float b_diff = fabs(b - b_host); | ||
|
|
||
| cout << "Difference in w: " << w_diff << ", b: " << b_diff << "\n"; | ||
|
|
||
| if (w_diff < 1e-10 && b_diff < 1e-10) | ||
| cout << "Verification PASSED.\n"; | ||
| else | ||
| cout << "Verification FAILED.\n"; | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # Compiler | ||
| CXX := g++ | ||
|
|
||
| # Compiler flags | ||
| CXXFLAGS := -Wall -Wextra -Werror -march=native -std=c++17 -O3 -fopenmp | ||
|
|
||
| # Executable name | ||
| EXEC := lr.out | ||
|
|
||
| # Source files | ||
| SRC_FILES := $(wildcard *.cpp) | ||
|
|
||
| # Dependancy | ||
| DEP := ../../../../util/ | ||
|
|
||
| .PHONY: all clean | ||
|
|
||
| all: $(EXEC) | ||
|
|
||
| $(EXEC): $(SRC_FILES) | | ||
| $(CXX) $(CXXFLAGS) -I$(DEP) -o $@ $^ | ||
|
|
||
| clean: | ||
| rm -rf $(EXEC) | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.