-
-
Notifications
You must be signed in to change notification settings - Fork 513
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
feat(RotationSystem): Deep Learning Support #5668
Draft
1zun4
wants to merge
17
commits into
nextgen
Choose a base branch
from
feat/deep-learning
base: nextgen
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.
+1,322
−48
Draft
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
b5f8564
feat(deeplearn): implement framework with model training
1zun4 155cf98
fix: missing lib
1zun4 3f2ef87
feat(deeplearn): entity distance delta and age
1zun4 ca463c4
fix(deeplearn): training data age inconsistency
1zun4 d7803f3
refactor(deeplearn): obsolete data generator
1zun4 30ac191
fix(HUD/Hotbar): overlay message formatting
1zun4 90d4729
feat(deeplearn): enhanced training and dataset
1zun4 f4ba535
fix(deeplearn/correction): none overwriting current
1zun4 d432838
fix(deeplearn/command): create, improve and delete command
1zun4 e445ca9
feat(deeplearn): measure prediction performance
1zun4 5521718
feat(DebugRecorder): buffered writing
1zun4 e11a4c2
Merge branch 'nextgen' into feat/deep-learning
1zun4 ee586fc
Merge branch 'nextgen' into feat/deep-learning
1zun4 7c6df0c
Merge branch 'nextgen' into feat/deep-learning
1zun4 eb7fb4a
Merge branch 'nextgen' into feat/deep-learning
1zun4 281c4ef
Merge branch 'nextgen' into feat/deep-learning
1zun4 b543439
fix(ConfigSystem): Load after client initialization
1zun4 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 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
This file contains 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
This file contains 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
81 changes: 81 additions & 0 deletions
81
src/main/kotlin/net/ccbluex/liquidbounce/deeplearn/DeepLearningEngine.kt
This file contains 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,81 @@ | ||
/* | ||
* This file is part of LiquidBounce (https://github.com/CCBlueX/LiquidBounce) | ||
* | ||
* Copyright (c) 2015 - 2025 CCBlueX | ||
* | ||
* LiquidBounce is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* LiquidBounce is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with LiquidBounce. If not, see <https://www.gnu.org/licenses/>. | ||
* | ||
* | ||
*/ | ||
package net.ccbluex.liquidbounce.deeplearn | ||
|
||
import ai.djl.engine.Engine | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import net.ccbluex.liquidbounce.config.ConfigSystem.rootFolder | ||
import net.ccbluex.liquidbounce.utils.client.logger | ||
import java.util.* | ||
|
||
object DeepLearningEngine { | ||
|
||
var isInitialized = false | ||
private set | ||
|
||
private val deepLearningFolder = rootFolder.resolve("deeplearning").apply { | ||
mkdirs() | ||
} | ||
|
||
val djlCacheFolder = deepLearningFolder.resolve("djl").apply { | ||
mkdirs() | ||
} | ||
|
||
val enginesCacheFolder = deepLearningFolder.resolve("engines").apply { | ||
mkdirs() | ||
} | ||
|
||
val modelsFolder = deepLearningFolder.resolve("models").apply { | ||
mkdirs() | ||
} | ||
|
||
init { | ||
System.setProperty("DJL_CACHE_DIR", djlCacheFolder.absolutePath) | ||
System.setProperty("ENGINE_CACHE_DIR", enginesCacheFolder.absolutePath) | ||
|
||
// Disable tracking of DJL | ||
System.setProperty("OPT_OUT_TRACKING", "true") | ||
|
||
ModelHolster | ||
} | ||
|
||
/** | ||
* DJL will automatically download engine libraries, as soon we call [Engine.getInstance()], | ||
* for the platform we are running on. | ||
* | ||
* This should be done here, | ||
* as we want to make sure that the libraries are downloaded | ||
* before we try to load any models. | ||
*/ | ||
suspend fun init() { | ||
logger.info("[DeepLearning] Initializing engine...") | ||
val engine = withContext(Dispatchers.IO) { | ||
Engine.getInstance() | ||
} | ||
val name = engine.engineName | ||
val version = engine.version | ||
val deviceType = engine.defaultDevice().deviceType.uppercase(Locale.ENGLISH) | ||
logger.info("[DeepLearning] Using engine $name $version on $deviceType.") | ||
isInitialized = true | ||
} | ||
|
||
} |
55 changes: 55 additions & 0 deletions
55
src/main/kotlin/net/ccbluex/liquidbounce/deeplearn/ModelHolster.kt
This file contains 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,55 @@ | ||
package net.ccbluex.liquidbounce.deeplearn | ||
|
||
import net.ccbluex.liquidbounce.config.types.Configurable | ||
import net.ccbluex.liquidbounce.deeplearn.DeepLearningEngine.modelsFolder | ||
import net.ccbluex.liquidbounce.deeplearn.models.MinaraiModel | ||
import net.ccbluex.liquidbounce.event.EventListener | ||
import net.ccbluex.liquidbounce.features.module.modules.render.ModuleClickGui | ||
import net.ccbluex.liquidbounce.utils.client.logger | ||
import kotlin.time.measureTimedValue | ||
|
||
object ModelHolster : EventListener, Configurable("DeepLearning") { | ||
|
||
/** | ||
* Dummy choice | ||
*/ | ||
val models = choices(this, "Model", 0) { | ||
arrayOf<MinaraiModel>(MinaraiModel("Empty", it)) | ||
} | ||
|
||
fun load() { | ||
logger.info("[DeepLearning] Loading models...") | ||
|
||
val choices = (modelsFolder.listFiles { file -> file.isDirectory }?.map { file -> | ||
val (model, time) = measureTimedValue { MinaraiModel(file.toPath(), models) } | ||
logger.info("[DeepLearning] Loaded model ${file.name} in ${time.inWholeMilliseconds}ms") | ||
model | ||
} ?: emptyList()).toMutableList() | ||
|
||
// We need a new instance of [NoneChoice] in order to trigger a changed event, | ||
// through [setByString] below - which is more of a hack and needs to be done properly in the future. | ||
models.choices = (listOf(MinaraiModel("Empty", models)) + choices).toMutableList() | ||
|
||
// Triggers a change event | ||
models.setByString(models.activeChoice.name) | ||
|
||
// Reload ClickGui | ||
ModuleClickGui.reloadView() | ||
} | ||
|
||
fun unload() { | ||
val iterator = models.choices.iterator() | ||
|
||
while (iterator.hasNext()) { | ||
val model = iterator.next() | ||
model.close() | ||
iterator.remove() | ||
} | ||
} | ||
|
||
fun reload() { | ||
unload() | ||
load() | ||
} | ||
|
||
} |
121 changes: 121 additions & 0 deletions
121
src/main/kotlin/net/ccbluex/liquidbounce/deeplearn/data/TrainingData.kt
This file contains 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,121 @@ | ||
package net.ccbluex.liquidbounce.deeplearn.data | ||
|
||
import com.google.gson.annotations.SerializedName | ||
import net.ccbluex.liquidbounce.config.gson.util.decode | ||
import net.ccbluex.liquidbounce.utils.aiming.data.Rotation | ||
import net.minecraft.util.math.Vec2f | ||
import net.minecraft.util.math.Vec3d | ||
import java.io.File | ||
|
||
/** | ||
* The age defines the ticks we start tracking the entity. However, due to the fact | ||
* that in simulated environments the time of combat will be much shorter, | ||
* and therefore not have enough data to satisfy our whole scale of combat time. | ||
* | ||
* This limit will allow the model to know that we just started, | ||
* until we reach the maximum training age. | ||
*/ | ||
const val MAXIMUM_TRAINING_AGE = 5 | ||
|
||
data class TrainingData( | ||
@SerializedName(CURRENT_DIRECTION_VECTOR) | ||
val currentVector: Vec3d, | ||
@SerializedName(PREVIOUS_DIRECTION_VECTOR) | ||
val previousVector: Vec3d, | ||
@SerializedName(TARGET_DIRECTION_VECTOR) | ||
val targetVector: Vec3d, | ||
@SerializedName(DELTA_VECTOR) | ||
val velocityDelta: Vec2f, | ||
|
||
@SerializedName(P_DIFF) | ||
val playerDiff: Vec3d, | ||
@SerializedName(T_DIFF) | ||
val targetDiff: Vec3d, | ||
|
||
@SerializedName(DISTANCE) | ||
val distance: Float, | ||
|
||
@SerializedName(HURT_TIME) | ||
val hurtTime: Int, | ||
/** | ||
* Age in this case is the Entity Age, however, we will use it later to determine | ||
* the time we have been tracking this entity. | ||
*/ | ||
@SerializedName(AGE) | ||
val age: Int | ||
) { | ||
|
||
val currentRotation | ||
get() = Rotation.fromRotationVec(currentVector) | ||
val targetRotation | ||
get() = Rotation.fromRotationVec(targetVector) | ||
val previousRotation | ||
get() = Rotation.fromRotationVec(previousVector) | ||
|
||
/** | ||
* Total delta should be in a positive direction, | ||
* going from the current rotation to the target rotation. | ||
*/ | ||
val totalDelta | ||
get() = currentRotation.rotationDeltaTo(targetRotation) | ||
|
||
/** | ||
* Velocity delta should be in a positive direction, | ||
* going from the previous rotation to the current rotation. | ||
*/ | ||
val previousVelocityDelta | ||
get() = previousRotation.rotationDeltaTo(currentRotation) | ||
|
||
val asInput: FloatArray | ||
get() = floatArrayOf( | ||
// Total Delta | ||
totalDelta.deltaYaw, | ||
totalDelta.deltaPitch, | ||
|
||
// Velocity Delta | ||
previousVelocityDelta.deltaYaw, | ||
previousVelocityDelta.deltaPitch, | ||
|
||
// Speed | ||
targetDiff.horizontalLength().toFloat() + playerDiff.horizontalLength().toFloat(), | ||
|
||
// Distance | ||
distance.toFloat() | ||
) | ||
|
||
val asOutput | ||
get() = floatArrayOf( | ||
velocityDelta.x, | ||
velocityDelta.y | ||
) | ||
|
||
companion object { | ||
const val CURRENT_DIRECTION_VECTOR = "a" | ||
const val PREVIOUS_DIRECTION_VECTOR = "b" | ||
const val TARGET_DIRECTION_VECTOR = "c" | ||
const val DELTA_VECTOR = "d" | ||
const val HURT_TIME = "e" | ||
const val AGE = "f" | ||
const val P_DIFF = "g" | ||
const val T_DIFF = "h" | ||
const val DISTANCE = "i" | ||
|
||
fun parse(vararg file: File): List<TrainingData> { | ||
val files = file.flatMap { f -> | ||
if (f.isDirectory) { | ||
f.listFiles { _, name -> name.endsWith(".json") }?.toList() | ||
?: emptyList() | ||
} else { | ||
listOf(f) | ||
} | ||
} | ||
|
||
return files.flatMap { file -> | ||
file.inputStream().use { stream -> | ||
decode<List<TrainingData>>(stream) | ||
} | ||
} | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do not review a draft pr