Skip to content
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

Add support for Alpine on LGBM #145

Merged
merged 28 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,28 @@ jobs:
- uses: codecov/codecov-action@v1
with:
fail_ci_if_error: true

build_alpine:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3
with:
fetch-depth: 0
- run: |
git fetch -f --tags
echo exit code $?
git tag --list

- name: Run tests on Alpine docker
run: docker run --rm -t -v $(pwd):/feedzai-openml-java -e FDZ_OPENML_JAVA_LIBC="musl" alpine:3.18.4 \
/bin/sh -c 'apk add clang openjdk8 maven bash && \
cd /feedzai-openml-java && \
mvn clean install -DskipTests && \
cp openml-lightgbm/lightgbm-builder/make-lightgbm/build/amd64/alpine/lib_lightgbm.so /lib/lib_lightgbm.so && \
mvn surefire:test -pl :openml-lightgbm,:openml-datarobot'

- uses: codecov/codecov-action@v1
with:
fail_ci_if_error: true
6 changes: 3 additions & 3 deletions openml-lightgbm/lightgbm-builder/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<groupId>com.feedzai.openml.lightgbm</groupId>
<artifactId>lightgbm-lib</artifactId>
<version>v0.1.0</version>
<version>v0.9.14</version>

<packaging>jar</packaging>
<name>Openml LightGBM lib</name>
Expand All @@ -33,8 +33,8 @@
<!-- Feedzai's FairGBM! -->
<lightgbm.repo.url>https://github.com/feedzai/fairgbm.git</lightgbm.repo.url>

<lightgbm.version>v0.1.0</lightgbm.version>
<lightgbmlib.version>v0.1.0</lightgbmlib.version>
<lightgbm.version>v0.9.14</lightgbm.version>
<lightgbmlib.version>v0.9.14</lightgbmlib.version>
</properties>

<build>
Expand Down
2 changes: 1 addition & 1 deletion openml-lightgbm/lightgbm-provider/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<description>OpenML LightGBM Machine Learning Model and Classifier provider</description>

<properties>
<lightgbmlib.version>v0.1.0</lightgbmlib.version>
<lightgbmlib.version>v0.9.14</lightgbmlib.version>
</properties>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.feedzai.openml.provider.lightgbm;

import java.io.IOException;

/**
* Enum that represents the infrastructure where code is running and consequent lgbm native libs locations.
*/
public class Infrastructure {

/**
* The CPU architecture used.
*/
private final CpuArchitecture cpuArchitecture;

/**
* The libc implementation available.
*/
private final LibcImplementation libcImpl;

public Infrastructure(CpuArchitecture cpuArchitecture, LibcImplementation libcImpl) {
artpdr marked this conversation as resolved.
Show resolved Hide resolved
this.cpuArchitecture = cpuArchitecture;
this.libcImpl = libcImpl;
}

/**
* Gets the native libraries folder name according to the cpu architecture.
artpdr marked this conversation as resolved.
Show resolved Hide resolved
*
* @return the native libraries folder name according to the cpu architecture.
*/
public String getLgbmNativeLibsFolder() throws IOException {


if (libcImpl == LibcImplementation.MUSL && cpuArchitecture == CpuArchitecture.AARCH64) {
throw new IOException("Trying to use LightGBM on a musl-based OS with unsupported arm64 architecture.");
}

final String libraryPath;
Renato2000 marked this conversation as resolved.
Show resolved Hide resolved
if (cpuArchitecture == CpuArchitecture.AARCH64) {
artpdr marked this conversation as resolved.
Show resolved Hide resolved
libraryPath = cpuArchitecture.getLgbmNativeLibsFolder() + "/";
}
else {
libraryPath = cpuArchitecture.getLgbmNativeLibsFolder() + "/" + libcImpl.getLibcImpl() + "/";
}

return libraryPath;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.feedzai.openml.provider.lightgbm;

/**
* Enum that represents the libc implementation available on the machine and consequent lgbm native libs locations.
*/
public enum LibcImplementation {
MUSL("musl"),
GLIBC("glibc");

/**
* This is the name of available libc implementation and indicates the folder where the lightgbm native libraries are.
*/
private final String libcImpl;

LibcImplementation(final String libcImpl){
this.libcImpl = libcImpl;
}

/**
* Gets the native libraries folder name according to the libc implementation.
*
* @return the native libraries folder name according to the libc implementation.
*/
public String getLibcImpl() {
return libcImpl;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,13 @@ static public void loadLibs() {

if (!libsLoaded) {
final CpuArchitecture cpuArchitecture = getCpuArchitecture(System.getProperty("os.arch"));
final LibcImplementation libcImpl = getLibcImplementation(System.getenv("FDZ_OPENML_JAVA_LIBC"));
artpdr marked this conversation as resolved.
Show resolved Hide resolved
final Infrastructure infrastructure = new Infrastructure(cpuArchitecture, libcImpl);

try {
loadSharedLibraryFromJar("libgomp.so.1.0.0", cpuArchitecture);
loadSharedLibraryFromJar("lib_lightgbm.so", cpuArchitecture);
loadSharedLibraryFromJar("lib_lightgbm_swig.so", cpuArchitecture);
loadSharedLibraryFromJar("libgomp.so.1.0.0", infrastructure);
loadSharedLibraryFromJar("lib_lightgbm.so", infrastructure);
loadSharedLibraryFromJar("lib_lightgbm_swig.so", infrastructure);
} catch (final IOException ex) {
throw new RuntimeException("Failed to load LightGBM shared libraries from jar.", ex);
}
Expand All @@ -82,22 +85,33 @@ static CpuArchitecture getCpuArchitecture(final String cpuArchName) {
}
}

static LibcImplementation getLibcImplementation(final String libcImpl) {
artpdr marked this conversation as resolved.
Show resolved Hide resolved
if (libcImpl == null || libcImpl.isEmpty()) return LibcImplementation.GLIBC;
artpdr marked this conversation as resolved.
Show resolved Hide resolved

try {
return LibcImplementation.valueOf(libcImpl.toUpperCase());
artpdr marked this conversation as resolved.
Show resolved Hide resolved
} catch (final IllegalArgumentException ex) {
logger.error("Trying to use LightGBM with an unsupported libc implementation {}.", libcImpl, ex);
throw ex;
}
}

/**
* Loads a single shared library from the Jar.
*
* @param sharedLibResourceName library "filename" inside the jar.
* @param cpuArchitecture cpu architecture.
* @param infrastructure infrastructure used composed of CPU architecture and libc implementation.
* @throws IOException if any error happens loading the library.
*/
static private void loadSharedLibraryFromJar(
final String sharedLibResourceName,
final CpuArchitecture cpuArchitecture
final Infrastructure infrastructure
) throws IOException {

logger.debug("Loading LightGBM shared lib: {} for {}.", sharedLibResourceName, cpuArchitecture);
artpdr marked this conversation as resolved.
Show resolved Hide resolved
final String libraryPath = infrastructure.getLgbmNativeLibsFolder() + sharedLibResourceName;

final InputStream inputStream = LightGBMUtils.class.getClassLoader()
.getResourceAsStream(cpuArchitecture.getLgbmNativeLibsFolder() + "/" + sharedLibResourceName);
.getResourceAsStream(libraryPath);
final File tempFile = File.createTempFile("lib", ".so");
final OutputStream outputStream = new FileOutputStream(tempFile);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* The copyright of this file belongs to Feedzai. The file cannot be
* reproduced in whole or in part, stored in a retrieval system,
* transmitted in any form, or by any means electronic, mechanical,
* photocopying, or otherwise, without the prior permission of the owner.
*
* © 2023 Feedzai, Strictly Confidential
*/
package com.feedzai.openml.provider.lightgbm;

import org.assertj.core.api.Assertions;
import org.junit.Test;

/**
* Tests the retrieval of libc implementation.
*
* @author Renato Azevedo ([email protected])
*/
public class TestLibcImplementation {
@Test
public void unknownLibcImplementationThrowsException() {
Assertions.assertThatThrownBy(() -> LightGBMUtils.getCpuArchitecture("klibc"))
Renato2000 marked this conversation as resolved.
Show resolved Hide resolved
.isInstanceOf(IllegalArgumentException.class);
}

@Test
public void defaultLibcImplementationIsGlibc() {
Assertions.assertThat(LightGBMUtils.getLibcImplementation(""))
.isEqualTo(LibcImplementation.GLIBC);

Assertions.assertThat(LightGBMUtils.getLibcImplementation(null))
.isEqualTo(LibcImplementation.GLIBC);
}

@Test
public void canReadKnownLibcImplementation() {
Assertions.assertThat(LightGBMUtils.getLibcImplementation("glibc"))
.isEqualTo(LibcImplementation.GLIBC);

Assertions.assertThat(LightGBMUtils.getLibcImplementation("musl"))
.isEqualTo(LibcImplementation.MUSL);
}
}
Loading