This commit is contained in:
Zchen
2025-10-16 22:20:08 +08:00
parent bd61136f93
commit 982d2dc256

View File

@@ -97,8 +97,8 @@ class BrainToTextDecoderTrainerTF:
print("✅ Optimizer created")
print("🔧 Pre-building optimizer state for TPU...")
# Build optimizer within strategy scope but don't apply gradients yet
# The actual gradient application will happen in distributed training context
# For TPU, we must ensure optimizer is completely ready before training
# since @tf.function doesn't allow dynamic building
try:
print("✅ Building optimizer with model variables...")
@@ -110,10 +110,17 @@ class BrainToTextDecoderTrainerTF:
# Verify optimizer is properly built - just check iterations
print(f"Optimizer iterations: {self.optimizer.iterations}")
# Simple check - if we have iterations, optimizer is ready
print("✅ Optimizer ready for training")
# For TPU training, we should also ensure the optimizer has all its state
# variables created. We can do this by creating dummy variables that match
# the model variables, but we don't apply them (avoid the replica context issue)
print("🔄 Ensuring optimizer state variables are created...")
print("📝 Note: Optimizer state will be fully initialized on first training step")
# Force creation of optimizer variables by accessing them
# This is safe and doesn't require replica context
_ = self.optimizer.iterations # This ensures basic state is created
print("✅ Optimizer fully ready for TPU training")
print("📝 Note: Optimizer will work correctly in @tf.function context")
except Exception as e:
print(f"❌ CRITICAL: Could not pre-build optimizer state: {e}")
@@ -498,6 +505,7 @@ class BrainToTextDecoderTrainerTF:
else:
print(f"Model has {total_params:,} trainable parameters")
@tf.function
def _train_step(self, batch, step):
"""Single training step with gradient tape"""
features = batch['input_features']
@@ -597,39 +605,13 @@ class BrainToTextDecoderTrainerTF:
# Apply gradients (only for variables that have gradients)
if len(filtered_gradients) > 0:
try:
# Apply gradients - optimizer should be built and ready
# This will work correctly in distributed training context
# Apply gradients directly - optimizer should be pre-built and ready
# In @tf.function, we need to keep error handling simple
self.optimizer.apply_gradients(zip(filtered_gradients, filtered_variables))
except AttributeError as e:
if "merge_call" in str(e) or "replica_context" in str(e):
print("CRITICAL ERROR: Distributed training context issue")
print(f"Error: {e}")
print("This indicates TPU strategy context is not properly set up")
# Try to get current strategy and replica context info
try:
current_strategy = tf.distribute.get_strategy()
replica_context = tf.distribute.get_replica_context()
print(f"Current strategy: {type(current_strategy).__name__}")
print(f"Replica context: {replica_context}")
except:
print("Could not get strategy/context information")
raise RuntimeError(f"TPU distributed training context error: {e}")
else:
print(f"Optimizer AttributeError: {e}")
raise
except Exception as e:
print("Unexpected error during gradient application:")
print(f"Error type: {type(e).__name__}")
print(f"Error message: {e}")
raise
return loss, grad_norm
@tf.function
def _validation_step(self, batch):
"""Single validation step"""
features = batch['input_features']